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

MissingReferenceException:

Discussion in 'Scripting' started by mindengine, Feb 4, 2009.

  1. mindengine

    mindengine

    Joined:
    Sep 8, 2008
    Posts:
    114
    I have a basic question about the best way to handle a bug like this one...

    .

    Basically I am destroying one object that is being reference by another object. What is the optimal way to unload the reference of the destroyed object??

    [/quote]
     
  2. Mike08

    Mike08

    Joined:
    Dec 29, 2005
    Posts:
    124
    Hi

    The error you get means that you try to access the deleted object with your reference to it.

    Your error is like this
    Code (csharp):
    1. Gameobject obj;
    2.  
    3. ....
    4.  
    5. do soth. with obj
    6.  
    When you then delete the GameObject that is referenced by obj then you get this error when you try to access it.

    To avoid this you can add this simple line of code into your script.

    Code (csharp):
    1. GameObject obj; //Your reference
    2.  
    3. ....
    4.  
    5. if(obj) //This here checks if the referenced object exists
    6. {
    7. do sth with obj
    8. }
    9.  
    When you destroy a object your reference to it is automatically set to null and you don't have to unload the reference manually.
    The reason is that your reference is a pointer to the object and so when you delete the object the pointer points to nowhere but you don't need to unload the pointer to make it point to nowhere.

    I hope this helped you to solve your problem

    By
     
  3. mindengine

    mindengine

    Joined:
    Sep 8, 2008
    Posts:
    114
    that did it! thanks so much.