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

Is detect game object destroyed in self component possible?

Discussion in 'Scripting' started by F, Jan 22, 2015.

  1. F

    F

    Joined:
    Sep 28, 2012
    Posts:
    13
    Code (CSharp):
    1. class MyComponent:MonoBehaviour
    2. {
    3.    public void DoSth()
    4.    {
    5.       if(gameObject==null)
    6.           return;
    7.       doSth...
    8.    }
    9. }
    For safe reason,I want the code itself can check its condition about whether it was destroyed, instead of check this by others. But this script will throw exception : You're trying access GameObject which is destroyed.

    what should I do?

    Thanks
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Use OnDestroy.
     
    F likes this.
  3. F

    F

    Joined:
    Sep 28, 2012
    Posts:
    13
    Good idea,feel myself idiot.