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

MissingReferenceException

Discussion in 'Scripting' started by corinne44, May 14, 2014.

  1. corinne44

    corinne44

    Joined:
    May 6, 2014
    Posts:
    9
    I keep getting an error whenever I press the "g" key that "The object of type 'GameObject has been destroyed but you are still trying to access it..."

    I'm sure there's a fairly simple fix to this. It says to check if null or not destroy the object, but I don't know how to implement this in my code. Any ideas?

    The error occurs at line 59 due to line 52's calling of the method DropItem()

    Code (csharp):
    1. import System.Collections.Generic;
    2.  
    3. class CItem
    4. {
    5.     var name : String;
    6.     var itemId : int;
    7.     var obj : GameObject;
    8.     var weight : float;
    9. }
    10.  
    11. var inventory : List.<CItem> = new List.<CItem>();
    12. var activeObject : int;
    13. var maxItems : int = 5;
    14. var pos : Vector2;
    15. var size : float;
    16. var spacing : float;
    17. var hand : GameObject;
    18. var dropPos : Vector3;
    19.  
    20. function Start ()
    21. {
    22.  
    23. }
    24.  
    25. function Update ()
    26. {
    27.    
    28.     activeObject = Mathf.Clamp (activeObject, 0, inventory.Count);
    29.    
    30.     if(Input.GetKeyDown("q"))
    31.     {
    32.         if(activeObject >= inventory.Count-1)
    33.         {
    34.             activeObject = 0;
    35.        
    36.         }else
    37.        
    38.             activeObject++;
    39.     }else
    40.    
    41.     if(Input.GetKeyDown("e"))
    42.     {  
    43.         if(activeObject <= 0)
    44.         {
    45.             activeObject = inventory.Count-1;
    46.         }else
    47.            
    48.             activeObject--;
    49.     }
    50.     if(Input.GetKeyDown("g"))
    51.     {
    52.         DropItem();
    53.     }
    54.    
    55. }
    56.  
    57. function DropItem()
    58. {
    59.     inventory[activeObject].obj.SetActive(true);
    60.     inventory[activeObject].obj.transform.position = dropPos + hand.transform.position;
    61.     inventory[activeObject].obj.transform.parent = null;
    62.     inventory[activeObject].obj.rigidbody.isKinematic = false;
    63.     inventory.Remove(inventory[activeObject]);
    64. }
    65.  
    66. function ItemPickup (item : GameObject)
    67. {
    68.         var newItem : CItem = new CItem();
    69.         newItem.name = item.transform.parent.gameObject.name;
    70.         newItem.obj = item.transform.parent.gameObject;
    71.         newItem.weight = item.transform.parent.gameObject.rigidbody.mass;
    72.         inventory.Add (newItem);
    73. }
    74.  
    75. function OnGUI()
    76. {
    77.     for(var i : int = 0; i < inventory.Count; i ++)
    78.     {
    79.         if(activeObject == i)
    80.         {
    81.             GUI.color = Color.red;
    82.         }
    83.         else
    84.             GUI.color = Color.white;
    85.        
    86.         GUI.Label (Rect(pos.x + (i * spacing) + 5, pos.y + 50, size, size/2), "" + inventory[i].name);
    87.         GUI.Box (Rect(pos.x + (i * spacing), pos.y, size, size), "");
    88.     }
    89. }
     
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Is the inventory empty?
     
  3. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    Code (csharp):
    1.  
    2. function DropItem()
    3.  
    4. {
    5.  
    6.     inventory[activeObject].obj.SetActive(true);
    7.  
    8.     inventory[activeObject].obj.transform.position = dropPos + hand.transform.position;
    9.  
    10.     inventory[activeObject].obj.transform.parent = null;
    11.  
    12.     inventory[activeObject].obj.rigidbody.isKinematic = false;
    13.  
    14.     inventory.Remove(inventory[activeObject]);
    15.  
    16. }
    17.  
    this code only works once, after that you need to change activeObject index so it will no longer point to the index of the item you removed from your inventory.

    try and reset to 0 each time you press to drop the selected item.
    Code (csharp):
    1.  
    2. if(Input.GetKeyDown("g"))
    3.     {
    4.         DropItem();
    5.         activeObject = 0;
    6.     }
    7.  
     
    Last edited: May 14, 2014
  4. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Even better would be to additionally modify your DropItem function to make sure your inventory List object contains a key that matches activeObject before attempting to access it. That way, even if the value somehow become invalid (such as dropping the last item) it wouldn't throw an error.
     
  5. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Plz, never write 'inventory[activeObject].obj.SetActive...' you will get a lot of ecxeption because of this. Add some checks or rewrite your code in other way. Until you have not sure if inventory is not null and activeObject is not null and inventory[activeObject].obj is not null, every part of this sentence could lead to exception.
     
  6. corinne44

    corinne44

    Joined:
    May 6, 2014
    Posts:
    9
    Patico, it doesn't make a difference if I write it as inventory[activeObject].obj.active = true; or as I did in my current code. That is not where the exception error is and changing it doesn't remove the error.
     
  7. corinne44

    corinne44

    Joined:
    May 6, 2014
    Posts:
    9
    Glockenbeat, nope. I can visibly see the two items in my inventory.

    ar0nax, I will try this ASAP and let you know if it worked :)

    wccrawford, I'm not exactly sure how I would go about this. What do you mean "contains a key"?
     
  8. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Hi, corinne44,
    You misunderstand me. I mean there are a lot of elements that could be null. If any one of inventory, activeObject or obj will be null you will got an NullReferenceExcemtion or, probably MissingReferenceException.

    It will be easier to find where is your error located, if you change this code
    Code (csharp):
    1.  
    2. function DropItem()
    3. {
    4.     inventory[activeObject].obj.SetActive(true);
    5.     inventory[activeObject].obj.transform.position = dropPos + hand.transform.position;
    6.     inventory[activeObject].obj.transform.parent = null;
    7.     inventory[activeObject].obj.rigidbody.isKinematic = false;
    8.     inventory.Remove(inventory[activeObject]);
    9. }
    10.  
    to this:
    Code (csharp):
    1.  
    2. function DropItem()
    3. {
    4.     var inventoryObj = inventory[activeObject];
    5.     if(inventoryObj != null)
    6.     {
    7.         var obj = inventoryObj.obj;
    8.  
    9.         if(obj != null)
    10.         {
    11.             obj.SetActive(true);
    12.             obj.transform.position = dropPos + hand.transform.position;
    13.             obj.transform.parent = null;
    14.             obj.rigidbody.isKinematic = false;
    15.             inventory.Remove(inventory[activeObject]);
    16.         }
    17.         else
    18.             Debug.Log("inventory[activeObject].obj == null");
    19.     }
    20.     else
    21.         Debug.Log("inventoryObj == null");
    22. }
    23.  
    Or will use Specification pattern:
    Code (csharp):
    1.  
    2.  
    3. function DropItem()
    4. {
    5.     if(IsInvertoryObjectExists())
    6.     {
    7.         inventory[activeObject].obj.SetActive(true);
    8.         inventory[activeObject].obj.transform.position = dropPos + hand.transform.position;
    9.         inventory[activeObject].obj.transform.parent = null;
    10.         inventory[activeObject].obj.rigidbody.isKinematic = false;
    11.         inventory.Remove(inventory[activeObject]);
    12.     }
    13. }
    14.  
    15. function IsInvertoryObjectExists() : bool
    16. {
    17.     if(inventory != null  activeObject != null)
    18.     {
    19.         if(inventory[activeObject] != null)
    20.         {
    21.             if(inventoryObj.obj != null)
    22.                 return true;
    23.             else
    24.                 Debug.Log("inventory[activeObject].obj == null");
    25.         }
    26.         else
    27.             Debug.Log("inventoryObj == null");
    28.     }
    29.     else
    30.         Debug.Log("inventory or activeObject is null");
    31.  
    32.     return false;
    33. }
    34.