I have a collectible item, which I can put into the inventory. Its GameObject has a InventoryItem script attached in which I want to set all parameters needed for correct display in the inventory. Here is the C# code:
InventoryItem.cs
public class InventoryItem : MonoBehaviour {
public bool collectible = true;
public string name;
public Sprite sprite;
}
Part of ItemCollectingController.cs responsible of passing data to InventoryController
if (Input.GetButtonDown("Use")) {
InventoryController inventory = GameObject.FindGameObjectWithTag("Inventory").GetComponent<InventoryController>();
inventory.AddItem(target.GetComponent<InventoryItem>());
Destroy(target);
Debug.Log("Added to inventory");
}
And the important part of InventoryController.cs
public List<InventoryItem> items;
public void AddItem(InventoryItem item) {
items.Add(item);
}
When collecting the item, the GameObject is destroyed and so is the reference in the InventoryController. The items List keeps a missing component. What can I do about that?