How to detect if a GameObject has been destroyed?

I have a script that does something like this (psuedo-code-ish):

var spawnedThing : GameObject;

function Update()
{
   if (whatever)
   {
      if (!spawnedThing || spawnedThing.isDestroyed)
          spawnedThing = InstantiateObject(...);

      spawnedThing.enabled;
   }
   else
   {
      if (spawnedThing && !spawnedThing.isDestroyed)
          spawnedThing.enabled = false;
   }   
}

My question is: how do I determine if spawnedThing has been destroyed? That is, my spawn script wants to…

  1. Spawn something if there isn’t already one.
  2. Enable/disable the spawned thing based on condition (eg, if player is too far away to notice.)
  3. Keep track of whether this one has been destroyed (i.e., player beat it up) and spawn a new one, if so.

When the GameObject is destroyed, does my variable go to null? Or do I have a pointer to a dead object? What’s best-practice, here?

Thanks!

After an object is destroyed, an equality check with `null` will return `true`. The variable does not go to null, you can still call `GetInstanceID()` on it, but the "==" operator is overloaded and behaves as expected.

I believe the following would do the trick

var spawnedThing : GameObject;

function Update()
{
   if (whatever)
   {
      if (spawnedThing == null)
          spawnedThing = InstantiateObject(...);

      spawnedThing.enabled;
   }
   else
   {
      if (spawnedThing != null)
          spawnedThing.enabled = false;
   }   
}

Edit: However, if you do this often, it might be a better idea to go easy on the garbage collector and reuse the objects. Instead of Destroying the spawnedThing when it gets killed, you can disable it and re-enable it whenever necessary. See this question for more details.

Edit2: If don't want to poll every frame, you could implement the OnDisable function in a component on spawnedThing. See this forum thread for more details on that.

Hi, I was also in the situation where I needed to know if a game object had been destroyed as opposed to the reference actually being null because I was running some code in OnDisable(), this is the solution I ended up with to test if a game object has been destroyed:

public static class GameObjectExtensions
{
        /// <summary>
        /// Checks if a GameObject has been destroyed.
        /// </summary>
        /// <param name="gameObject">GameObject reference to check for destructedness</param>
        /// <returns>If the game object has been marked as destroyed by UnityEngine</returns>
        public static bool IsDestroyed(this GameObject gameObject)
        {
            // UnityEngine overloads the == opeator for the GameObject type
            // and returns null when the object has been destroyed, but 
            // actually the object is still there but has not been cleaned up yet
            // if we test both we can determine if the object has been destroyed.
            return gameObject == null && !ReferenceEquals(gameObject, null);
        }
 }

Instead of polling every frame whether the object is destroyed, it's probably better just to have a callback which the object calls when it's destroyed.

i think the best way to do so is to use if(spawnedThing),the best way is to use if(spawnedThing) to check if it’s null