Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to check if a destroyed GameObject isn't allive

Discussion in 'Scripting' started by Gistiv, Jun 20, 2014.

  1. Gistiv

    Gistiv

    Joined:
    Apr 22, 2014
    Posts:
    13
    Hi folks,

    I'm looking for a way to check if a gameobject is allive or not...
    In my game I've got a Tower which is shooting on a enemy and if his health is zero the enemy dies, but i dont know how to delete the bullets which are focused on the enemie but cant hit it anymore because it has been destroyed....

    I tried to check it like that:
    Code (CSharp):
    1. if(enemy == null)
    2.      Destroy(bullet);
    3.  
    In many forums I'd thought to read that this should work but it doesn't...

    I hope you can help me :))
     
  2. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    http://docs.unity3d.com/430/Documentation/ScriptReference/Object.Destroy.html

    "Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering."

    Tho, unity uses a fake null object to represent destroyed/empty objects. The question in place would be now if it acctually sets the object to be null instantly or just after the update loop.
     
  3. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    If your bullet is focused on the enemy, and I mean that variable enemy is a bullet's property. Then you can put in bullet's update cycle something like this:

    Code (CSharp):
    1.  
    2. if (enemy == null) {
    3.   Destroy (gameObject);
    4. }
    5.  
    1) Cannon chooses an enemy to shoot.
    2) Cannon spawns a bullet.
    3) Cannon set's bullet's enemy property accordingly to selected enemy.
    4) Every frame bullet is checking if enemy is null.
    5a) If enemy is gone, then bullet commits suicide.
    5b) If enemy exists, then bullet moves towards the enemy.
     
  4. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    I prefer eventing for things like this. When an enemy dies, I make it call an OnDeath event before destruction. Bullets that focus that enemy can just subscribe a SelfDestruct method to their target's OnDeath and destroy themselves automatically when their target dies.
     
    Sharp-Development likes this.
  5. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Thats acctually a quiet perfect setup.
     
  6. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    True. Event system is a hard thing for a starter, but will provide an excellent, fast performance in a scene crowded with projectiles.
     
  7. Gistiv

    Gistiv

    Joined:
    Apr 22, 2014
    Posts:
    13
    Thanks for the quick answers!
    The problem is that the bullet which destroys the enemy is destoryed too but not the ones which are still in the air which don't know that their target is destroyed....
    I guess i'll try the event thing!