Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Inventory : MonoBehaviour { public GameObject inventory; private bool inventoryEnabled; private int allSlots; private int enableSlots; private GameObject[] slot; public GameObject InventorySlots; void Start() { allSlots = 30; slot = new GameObject[allSlots]; for (int i = 0; i < allSlots; i++) { slot[i] = InventorySlots.transform.GetChild(i).gameObject; if(slot[i].GetComponent<Slot>().item == null) { slot[i].GetComponent<Slot>().empty = true; } } } void Update() { if (Input.GetKeyDown(KeyCode.I)) inventoryEnabled = !inventoryEnabled; if (inventoryEnabled == true) { inventory.SetActive(true); } else { inventory.SetActive(false); } } void ItemPickedUp(GameObject PickMeUp) { GameObject itemPickedUp = PickMeUp.gameObject; Item item = itemPickedUp.GetComponent<Item>(); AddItem(itemPickedUp, item.ID, item.type, item.description, item.icon); } void AddItem(GameObject itemObject, int itemID, string itemType, string itemDescription, Sprite itemIcon) { for (int i = 0; i < allSlots; i++) { if (slot[i].GetComponent<Slot>().empty) { itemObject.GetComponent<Item>().pickedUp = true; slot[i].GetComponent<Slot>().item = itemObject; slot[i].GetComponent<Slot>().icon = itemIcon; slot[i].GetComponent<Slot>().type = itemType; slot[i].GetComponent<Slot>().ID = itemID; slot[i].GetComponent<Slot>().description = itemDescription; itemObject.transform.parent = slot[i].transform; itemObject.SetActive(false); slot[i].GetComponent<Slot>().UpdateSlot(); slot[i].GetComponent<Slot>().empty = false; } return; } } } what is the problem and how do i fix it? everything work fine, but i get that annoying message
Remove the return. Otherwise the code will only run for the first "slot". Code (CSharp): void AddItem(GameObject itemObject, int itemID, string itemType, string itemDescription, Sprite itemIcon) { for (int i = 0; i < allSlots; i++) { if (slot[i].GetComponent<Slot>().empty) { itemObject.GetComponent<Item>().pickedUp = true; slot[i].GetComponent<Slot>().item = itemObject; slot[i].GetComponent<Slot>().icon = itemIcon; slot[i].GetComponent<Slot>().type = itemType; slot[i].GetComponent<Slot>().ID = itemID; slot[i].GetComponent<Slot>().description = itemDescription; itemObject.transform.parent = slot[i].transform; itemObject.SetActive(false); slot[i].GetComponent<Slot>().UpdateSlot(); slot[i].GetComponent<Slot>().empty = false; } } }
Oh I see you are trying to add an item to the first empty slot. Sorry I didn't think it through. You want to place your return in the if() block instead: Code (CSharp): void AddItem(GameObject itemObject, int itemID, string itemType, string itemDescription, Sprite itemIcon) { for (int i = 0; i < allSlots; i++) { if (slot[i].GetComponent<Slot>().empty) { itemObject.GetComponent<Item>().pickedUp = true; slot[i].GetComponent<Slot>().item = itemObject; slot[i].GetComponent<Slot>().icon = itemIcon; slot[i].GetComponent<Slot>().type = itemType; slot[i].GetComponent<Slot>().ID = itemID; slot[i].GetComponent<Slot>().description = itemDescription; itemObject.transform.parent = slot[i].transform; itemObject.SetActive(false); slot[i].GetComponent<Slot>().UpdateSlot(); slot[i].GetComponent<Slot>().empty = false; return; } } }
For performance you should only use GetComponent once and cache it: Code (CSharp): for (int i = 0; i < allSlots; i++) { Slot tempSlot = slot[i].GetComponent<Slot>(); if (tempSlot.empty) { itemObject.GetComponent<Item>().pickedUp = true; tempSlot.item = itemObject; tempSlot.icon = itemIcon; tempSlot.type = itemType; tempSlot.ID = itemID; tempSlot.description = itemDescription; itemObject.transform.parent = slot[i].transform; itemObject.SetActive(false); tempSlot.UpdateSlot(); tempSlot.empty = false; return; } }