Destroy() not firing anymore after adding reference from other script

I was just fooling around with the space tutorial, and while trying to figure out how to get a destroyed asteroid event to talk to the gamecontroller to alter the score (or kill the player) - the Destroy() that used to work now doesn’t.

Current code:

   if (other.tag == "Player")
        {
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
            gameController.GetComponent<GameController>().asteroidCollidedEvent();
            Destroy(other.gameObject);
           
        }
        else
        {
            Instantiate(explosion, transform.position, transform.rotation);
            gameController.GetComponent<GameController>().asteroidShotEvent();
            Destroy(this.gameObject);
        }

Old (working) code:

  if (other.tag == "Player")
        {
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
   
            Destroy(other.gameObject);
           
        }
        else
        {
            Instantiate(explosion, transform.position, transform.rotation);
            Destroy(this.gameObject);
        }

Any idea why this is happening and how to fix this? Thanks again!

I assume you are not seeing any exceptions thrown by the above event (or the other one)?

Make sure your red bubble (errors) is enabled on the console, or else your exceptions/errors will be invisible.

Hey thanks for the reply - riiiight I should have been looking there:

NullReferenceException: Object reference not set to an instance of an object
DestroyOnCollide.OnTriggerEnter (UnityEngine.Collider other)

So GameController is always in existence, do I need to maybe assign this:
gameController.GetComponent().asteroidShotEvent();

to an object in the script?

Ah this is what I was missing:

 void Start ()
    {
        gameController = GameObject.Find("GameController");
    }
1 Like

I love it when a script comes together. Excellent!