Hola a todos,
I have an Inventory class, that represents my inventory (duh :p) with a List (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
void FixedUpdate(){
if (Input.GetKey (KeyCode.F)) {
//focus keeps the reference to the item i'm picking
if (focus != null) {
myInventory.PickupItem (focus);
Debug.Log (focus);
Destroy (focus.gameObject);
focus = null;
}
}
}
Inventory
public class Inventory {
private List<Item> itemList;
public Inventory(){
itemList = new List<Item>();
}
public void PickupItem(Item item){
itemList.Add (item);
}
public List<Item> GetItems(){
return itemList;
}
}
WorldController
public class WorldController : MonoBehaviour {
public GameObject inventoryPanel;
public GameObject player;
private Inventory inventory;
public void Start(){
inventory = player.GetComponent<PlayerController> ().GetInventory();
}
public void Update(){
UpdatePanel ();
}
public void UpdatePanel(){
int i = 0;
for (i=0; i < inventoryPanel.transform.childCount; i++) {
Transform panel = inventoryPanel.transform.GetChild (i);
Item anItem = (i < inventory.GetItems().Count) ?
inventory.GetItems()[i] : null;
Debug.Log (i < inventory.GetItems ().Count);
//This goes true when something's picked
Debug.Log("Total :" + inventory.GetItems().Count +
" - - " + (anItem!=null?anItem.name:"Nada"));
//Having some items keep logging "Nada"
panel.FindChild ("Item #").GetComponent<Text> ().text = anItem != null?anItem.Name:"";
}
}
}
Thank you all, I hope my english is ok.