Disclaimer: I'm relatively new to developing games so bear with me.
In my scene I have enemies and when they die, they drop items. No matter which item is dropped, the same prefab (the physical representation of the dropped item) is instantiated. That physical orb on the ground may represent different items. It could be a pile of coins, a sword, etc. Depending on which item is dropped, I want its name to appear in a UI label attached to the top of the item. Basic ARPG stuff really. The problem I'm running into is that I can't seem to get the references right.
DisplayItemNameLabel is a script which is attached to each item when it is dropped. I did it this way so each item can be in charge of its own UI label.
ItemDrops is attached to each enemy. SpawnItem(Item item) is a function which is called when the enemy dies.
The last relevant Monobehaviour script is the ItemData script which is attached to an Item Manager game object which is there from the beginning and persists forever. This class just defines the different items that are possible in the game.
So basically the structure is that when an enemy dies, if the random number generated is within some range, then some item will drop.
If (randomNumber < 0.5)
where SpawnItem(Item item) is something like...
private void SpawnItem(Item item)
itemDropped = true;
nameOfDroppedItem = item.itemName;
Instantiate(itemPrefab, transform.position - offset, Quaternion.identity);
Again, this is attached to each enemy.
In the DisplayItemLabel script then, I would like to set itemNameText.text to that nameOfDroppedItem. But the DisplayItemLabel script is attached to each item. I don't think I can make each item a child of each enemy because enemies are destroyed when they are killed so the scripts would be destroyed. I also can't use
GameObject.FindGameObjectWithTag("Enemy").GetComponent<ItemDrops>(); because it wouldn't always grab the enemy I want. I thought maybe I could set the text in SpawnItem but it would have to reference the correct item, so I couldn't use GameObject.FindGameObjectWithTag...
What should I do?