Search Unity

How does Unity implement nulling references?

Discussion in 'Scripting' started by magwo, Jan 3, 2010.

  1. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    So when you Destroy a game object.. any and all references to it turn null. How exactly does Unity implement this functionality? Is it a built-in .Net feature that anyone can use, or is it just a reference registry in each game object somehow?
     
  2. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    Bump.
     
  3. Lucas Meijer_old

    Lucas Meijer_old

    Joined:
    Apr 5, 2008
    Posts:
    436
    It's a special thing we implemented (and which is only turned on in the editor). When you Object.Destroy() a unity object, the managed wrapper (the .net object you usually interact with), is not thrown away. instead the managed wrapper realizes that the object it wraps is destroyed, and informs you of that fact when you try to do anything with it.

    When you have no more references to the .net object, and a new garbage collector run happens, the actual wrapper object itself will be cleaned up.

    Bye, Lucas
     
  4. Joe-Robins

    Joe-Robins

    Unity Technologies

    Joined:
    Apr 21, 2008
    Posts:
    430
  5. magwo

    magwo

    Joined:
    May 20, 2009
    Posts:
    402
    Alrighty, so basically.. every call you make on a gameobject or its components.. starts with some kind of null reference check against the unmanaged object?
     
  6. snoopbaron

    snoopbaron

    Joined:
    Mar 21, 2009
    Posts:
    88
    I just tested this but got the same behavior in the web player as in the editor.

    I created two Spheres. One sphere had the following script:

    Code (csharp):
    1. function OnMouseDown() { Destroy(gameObject); }
    The other sphere had this script:

    Code (csharp):
    1. var go : GameObject; // set this to the other sphere
    2.  
    3. var go : GameObject; // set to the other sphere
    4.  
    5. var doneOnce = false;
    6. function Update() {
    7.   if (go == null  !doneOnce) {
    8.     doneOnce = true;
    9.     GameLog.GetInstance().Info("go == null"); // my gui log
    10.   }
    In both cases the "go" reference was set to null automatically when I clicked on the sphere that "go" referred to.

    What's the expected behavior from Unity? Is this a bug or a feature working as expected?

    I also posted this on Unity Answers.
     
  7. Per

    Per

    Joined:
    Jun 25, 2009
    Posts:
    457
    I'd imagine that's the expected behavior, it's a "smart pointer".
     
  8. snoopbaron

    snoopbaron

    Joined:
    Mar 21, 2009
    Posts:
    88
    I think so to. It certainly is handy and it is a good idea to keep behavior in the editor as consistent as possible with deployment behavior. I'd like to get confirmatiin from unity :)
     
  9. BillyMFT

    BillyMFT

    Joined:
    Mar 14, 2013
    Posts:
    178
    Can we get the direct link please? I can't find the answer.
     
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    15,349
unityunity