Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

So the 'destroyedGameObject == null' will not return true now?

Discussion in 'Scripting' started by ookk47oo, Jul 2, 2019.

  1. ookk47oo

    ookk47oo

    Joined:
    Mar 17, 2017
    Posts:
    80
    Code (CSharp):
    1. public class testobj : MonoBehaviour {
    2.     // Use this for initialization
    3.     void Start () {
    4.         Test();
    5.     }
    6.    
    7.     public void Test()
    8.     {
    9.         Destroy(gameObject);
    10.         bool bNull = gameObject == null;
    11.         bool bEqualNull = object.Equals(gameObject, null);
    12.         var rb = gameObject.GetComponent<Rigidbody2D>();
    13.         rb.useAutoMass = false;
    14.         Debug.Log(bNull.ToString() + bEqualNull.ToString());
    15.     }
    16. }
    I test the code in Unity 2017 and it prints 'FalseFalse'.It does not even throw MissingReferenceException for the GetComponent method which I remembered will happen in the old version.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    No. Game object will be destroyed when your method will exit.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    That's wrong.

    It will get destroyed at the end of the frame. It happens around where OnDestroy happens here.
     
    palex-nx likes this.
  4. ookk47oo

    ookk47oo

    Joined:
    Mar 17, 2017
    Posts:
    80
    I see. Thx:)