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. Dismiss Notice

Check if gameobject is destroyed

Discussion in 'Scripting' started by Earthquake, Aug 30, 2011.

  1. Earthquake

    Earthquake

    Joined:
    Nov 2, 2010
    Posts:
    261
    How do you check if something is destroyed.
    Like if you win a mission in game you have to kill something. And the "mission manager" checks to see if it is destroyed.
    Thanks
     
  2. handsomePATT

    handsomePATT

    Joined:
    Nov 30, 2010
    Posts:
    574
    check if its null
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    To expand on that
    Code (csharp):
    1.  
    2. if (theObject == null) {
    3.     Debug.Log ("Its destroyed");
    4. }
    5.  
    Though in the example you gave, usually the 'something' would send a message to the mission manager instead of it checking blindly every frame.
     
  4. _MGB_

    _MGB_

    Joined:
    Apr 24, 2010
    Posts:
    74
    necro-ing this for correctness :p

    Note: GameObjects get destroyed at the end of the current frame by default (unless you choose the immediate option, which is not advisable). Therefore checks against null in the same frame will not work.
     
  5. NathanJSmith

    NathanJSmith

    Joined:
    May 11, 2018
    Posts:
    57
    It's true, my current solution is have a flag to mark if object is destroyed
    Code (CSharp):
    1. bool m_isDestroy = false;
    2.  
    3. //Force/remind coder to use this function for destroying
    4. public void Destroy()
    5. {
    6.     if(!m_isDestroy)
    7.     {
    8.         GameObject.Destroy(gameObject);
    9.         m_isDestroy = true;
    10.     }  
    11. }
    12.  
    13. //Use this function to check
    14. public bool IsDestroy
    15. {
    16.     get { return m_isDestroy; }
    17. }
    Not the best because I can't programmatically force coder to use that `Destroy()` function for destroying.
     
    shieldgenerator7 and AL-KAED like this.