#Object reference error with OnTriggerExit2D and Enter2D

1 messages · Page 1 of 1 (latest)

granite gyro
#
using System.Collections.Generic;
using UnityEngine;

public class triggerForMasterSpawner : MonoBehaviour
{
    public spawnerScript spawnHim;
    

    void Start()
    {
        spawnHim = GetComponent<spawnerScript>();
    }


    void OnTriggerEnter2D(Collider2D insideObject)
    {
        if (insideObject.tag == "barg")
        {
            spawnHim.enabled = false;
            Debug.Log("BOSS IS HERE!!!");
        }
        else if (spawnHim == null)
        {
            Debug.Log("wtf is happening");
        }

    }
    void OnTriggerExit2D(Collider2D leaveObject)
    {
        if(leaveObject.tag != "barg")
        {
          spawnHim.enabled = true;
          Debug.Log("BOSS HAS LEFT!!!");
        }
        else if (spawnHim == null)
        {
            Debug.Log("wtf is happening");
        }
    }

    void Update()
    {
        
    }
}```
I'm trying to enable a spawner whenever my boss leaves  the check area and disable the spawner whenever the boss enters. I have a 2D collider covering the play area to check for the boss, both colliders are set to trigger, and one has a rigidbody2D. The errors happen at spawnHim.enabled = false; and spawnHim.enabled = true;
south coral
#

spawnHim = GetComponent<spawnerScript>(); is probably not getting anything

#

step through the code and look at if spawnHim is actually being set

granite gyro
#

thanks, fixed it 👍