Search Unity

go1=go2; Destroy(go2) and still have go1 != null

Discussion in 'Scripting' started by EugeneKiver, Jan 4, 2012.

  1. EugeneKiver

    EugeneKiver

    Joined:
    Jan 4, 2012
    Posts:
    5
    Hi,
    I suppose its rather general C# scripting question then unity related but still.
    I have two classes

    Code (csharp):
    1. public class Item
    2. {
    3. [INDENT]// Icon for inventory (drag'n'dropped in editor from prefab)[/INDENT]
    4. [INDENT]public gameObject icon;[/INDENT]
    5.  
    6. [INDENT]// When Clicked[/INDENT]
    7. [INDENT]OnInput[/INDENT]
    8. [INDENT]{[/INDENT]
    9. [INDENT][INDENT]inventory.AddItem(gameObject) // In this case everything's OK[/INDENT][/INDENT]
    10. [INDENT][INDENT]Destroy(gameObject)[/INDENT][/INDENT]
    11. [INDENT]}[/INDENT]
    12.  
    13. }

    Code (csharp):
    1. public class Inventory
    2. {
    3. [INDENT]public GameObject itemToInventory;[/INDENT]
    4. [INDENT]private GameObject grabbedItem;[/INDENT]
    5.  
    6. [INDENT]OnInput ()[/INDENT]
    7. [INDENT]{[/INDENT]
    8. [INDENT][INDENT]AddItem(grabbedItem); // In this case I'll get an exception [/INDENT][/INDENT]
    9. [INDENT][INDENT]Destroy(grabbedItem);[/INDENT][/INDENT]
    10. [INDENT]}[/INDENT]
    11. [INDENT]public void AddItem (GameObject item)[/INDENT]
    12. [INDENT]{[/INDENT]
    13. [INDENT][INDENT]if (item.GetComponent<Item>() != null)[/INDENT][/INDENT]
    14. [INDENT][INDENT]{[/INDENT][/INDENT]
    15. [INDENT][INDENT][INDENT]itemToInventory = item.GetComponent<Item>().icon; // In this case everything is FINE[/INDENT][/INDENT][/INDENT]
    16. [INDENT][INDENT]} else[/INDENT][/INDENT]
    17. [INDENT][INDENT]{[/INDENT][/INDENT]
    18. [INDENT][INDENT][INDENT]itemToInventory = item; // In this case I'll get exception on instantiation[/INDENT][/INDENT][/INDENT]
    19. [INDENT][INDENT]}[/INDENT][/INDENT]
    20. [INDENT][INDENT]//StartDelay()// And call CallAfterAnimation() after its over[/INDENT][/INDENT]
    21. [INDENT]}[/INDENT]
    22. [INDENT]OnUpdate[/INDENT]
    23. [INDENT]{[/INDENT]
    24. [INDENT][INDENT]//When delay is over call CallAfterAnimation()[/INDENT][/INDENT]
    25. [INDENT]}[/INDENT]
    26.  
    27. [INDENT]// Called with delay[/INDENT]
    28. [INDENT]public void CallAfterAnimation[/INDENT]
    29. [INDENT]{[/INDENT]
    30. [INDENT][INDENT]Instantiate(itemToInventory)[/INDENT][/INDENT]
    31. [INDENT]}[/INDENT]
    32.  
    33. }
    So the thing is that when I'm saving temporary item from Item Class (through AddItem() in Inventory)
    Code (csharp):
    1. itemToInventory = item.GetComponent<Item>().icon;
    and in the next moment destroying this gameObject Destroy(Item), itemToInventory stays valid (not null) and after delay
    Code (csharp):
    1. Instantiate(itemToInventory)
    works just fine. But when using Inventory's class own method OnInput to save object like following (again through Inventory's AddItem() )
    Code (csharp):
    1. itemToInventory = item;
    then calling Destroy(item) makes itemToInventory == null and after delay there's nothing to instantiate from so I'm getting exception at final method CallAfterAnimation().
    The question is: how to save properly gameObject in itemToInventory variable to keep in not null even after destroying the object from which it got all the data?
     
  2. kt1117

    kt1117

    Joined:
    Jan 2, 2012
    Posts:
    6
    I think you are able to keep this data in a temp object, created before destroy your object. Just create a temp object, and write code with static variables, and overwrite this with your data.
     
  3. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Saying g01 = g02 doesn't make a new object. It's just two references to the same object. If you destroy the gameObject then both references will be null.

    In the working case you say :

    itemToInventory = item.GetComponent<Item>().icon;

    so when you destroy the gameObject "item" the gameObject "icon" is still around.
     
  4. EugeneKiver

    EugeneKiver

    Joined:
    Jan 4, 2012
    Posts:
    5
    But documentation says that in my case game obj its components and transform children will be destroyed, how comes that icon survives this impact? And one more q, I have to call go1 = instantiate(go2) in order to make copy that won't be destroyed on destroy(go2) ?
    Thank you