Question by
Masslr · Jan 23 at 08:19 PM ·
pickupinventory systemitemitemsitem pickup
Item is missing in inventory slot after i pick up and delete the item. C#
Helloo, I'm working on a inventory for my game, and i can pick up items that will be put in the inventory. After i pick it up i see the image in the inv, but after deleting the object from the scene i think the inventory cant find the Item script anymore. so now other items overwrite the previous item. How can i make sure the Slot keeps the original Item?
public class InventoryManager : MonoBehaviour
{
Slot[] slots = new Slot[5];
void Awake()
{
for (int i = 0; i < 5; i++)
{
slots[i] = GameObject.Find("Slot-"+i).GetComponent<Slot>();
Debug.Log(i);
}
}
public void Add_Item(Item item)
{
foreach (var slot in slots)
{
if (slot.item == null)
{
slot.item = item;
slot.gameObject.GetComponent<Image>().sprite = item.gameObject.GetComponent<Image>().sprite;
break;
}
}
}
}
//Pick up script
void OnTriggerEnter(Collider col)
{
if (col.tag == "Player")
{
GameObject.Find("Library").GetComponent<InventoryManager>().Add_Item(item);
Destroy(gameObject);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
the unable stack item(equip) change to stack item(consume) and amount increase. 0 Answers
Inventory System with Randomly Generated Loot 2 Answers
Programmatically retrieving dynamic variable information from another script. 1 Answer
My Inventory system is in a big mess now, help me, please 0 Answers
Help with a universal item script 0 Answers