Search Unity

Losing variable reference somewhere?

Discussion in 'Scripting' started by Puyol, Feb 12, 2016.

  1. Puyol

    Puyol

    Joined:
    Nov 2, 2015
    Posts:
    3
    Hola a todos,

    I have an Inventory class, that represents my inventory (duh :p) with a List<Items> (another class)
    My Player Controller Script has an Inventory. Then, I have a WorldController class that uses the Player reference to reach his inventory, and update several panel, showing these items.

    Now, the WorldController script knows my inventory has some items (itemList.Count > 0) but when accessing the List childs, they're all null.
    Maybe i'm losing the reference someewhere?

    I leave you the important code.

    Player Controller
    Code (CSharp):
    1. void FixedUpdate(){
    2.         if (Input.GetKey (KeyCode.F)) {
    3.            //focus keeps the reference to the item i'm picking
    4.             if (focus != null) {
    5.                 myInventory.PickupItem (focus);
    6.                 Debug.Log (focus);
    7.                 Destroy (focus.gameObject);
    8.                 focus = null;
    9.             }
    10.       }
    11. }
    Inventory
    Code (CSharp):
    1. public class Inventory {
    2.    
    3.     private List<Item> itemList;
    4.  
    5.     public Inventory(){
    6.         itemList = new List<Item>();
    7.     }
    8.  
    9.         public void PickupItem(Item item){
    10.         itemList.Add (item);
    11.     }
    12.  
    13.     public List<Item> GetItems(){
    14.         return itemList;
    15.     }
    16. }
    WorldController
    Code (CSharp):
    1. public class WorldController : MonoBehaviour {
    2.  
    3.     public GameObject inventoryPanel;
    4.     public GameObject player;
    5.  
    6.     private Inventory inventory;
    7.  
    8.     public void Start(){
    9.         inventory = player.GetComponent<PlayerController> ().GetInventory();
    10.     }
    11.  
    12.     public void Update(){
    13.         UpdatePanel ();
    14.     }
    15.  
    16.     public void UpdatePanel(){
    17.         int i = 0;
    18.         for (i=0; i < inventoryPanel.transform.childCount; i++) {
    19.             Transform panel = inventoryPanel.transform.GetChild (i);
    20.             Item anItem = (i < inventory.GetItems().Count) ?
    21.                                 inventory.GetItems()[i] : null;
    22.             Debug.Log (i < inventory.GetItems ().Count);
    23.              //This goes true when something's picked
    24.             Debug.Log("Total :" + inventory.GetItems().Count +
    25.                     " - - " + (anItem!=null?anItem.name:"Nada"));
    26.             //Having some items keep logging "Nada"
    27.             panel.FindChild ("Item #").GetComponent<Text> ().text = anItem != null?anItem.Name:"";
    28.         }
    29.     }
    30. }
    Thank you all, I hope my english is ok.
     
  2. Puyol

    Puyol

    Joined:
    Nov 2, 2015
    Posts:
    3
    Ok, so, playing a bit with the variables, I found that when I call 'Destroy' on the focus variable, it destroys everything even the Item reference despite I'm keeping it in a Collection.

    Resolved by SetActive(false) the gameobject.