Loading a Sprite from an Inventory System

I’m following [this tutorial][1] for an Inventory system, and I have it set up so that I can call “AddItem” based on certain win conditions, but what I’d like to do is load the sprite for the item that’s just been added, onto a sort of scorecard that flashes at the end of a round… essentially, you are getting the item as a reward for doing well, and I need the sprite to display on the scorecard, as well as populating into the inventory. The item being added is grabbed randomly, as well.

Here’s my AddItem code:

public void AddItem(int id)
		Item itemToAdd = database.FetchItemByID (id);
		if (itemToAdd.Stackable && CheckIfInInventory(itemToAdd)) {
			for (int i = 0; i < items.Count; i++) {
				if (items [id].ID == id) {
					ItemData data = slots *.transform.GetChild (0).GetComponent<ItemData> ();*
  •  			data.amount++;*
  •  			data.transform.GetChild (0).GetComponent<Text> ().text = data.amount.ToString();*
  •  			break;*
  •  		}*
  •  	}*
  •  }*
  •  else {*
  •  	for (int i = 0; i < items.Count; i++) {*

_ if (items .ID == -1) {_
_ items = itemToAdd;
* GameObject itemObj = Instantiate (inventoryItem);
itemObj.GetComponent ().item = itemToAdd;
itemObj.GetComponent ().amount = 1;
itemObj.GetComponent ().slot = i;
itemObj.transform.position = Vector2.zero;
itemObj.GetComponent().sprite = itemToAdd.Sprite;
itemObj.name = itemToAdd.Title;
And my end-of-round conditional, which is in a GameController (and being called through inv.AddItem):
if (dataController.showPlayerScore () > 99) {
Debug.Log (“Excellent!”);
scoreGrade1.SetActive (true);
itemAdd.gameObject.SetActive (true);
inv.AddItem (Random.Range(0, 8));
Debug.Log (“Item Added”);
} else if (dataController.showPlayerScore () < 21) {
Debug.Log (“Yikes!”);
scoreGrade3.SetActive (true);
itemMiss.gameObject.SetActive (true);
} else {
Debug.Log (“Not Bad!”);
scoreGrade2.SetActive (true);
itemMiss.gameObject.SetActive (true);
[1]: Inventory System Tutorial in Unity 5 - PART 1 - YouTube

So if I’m understanding this correctly, your item list is a fixed length List, and instead of “Adding” a new item, you are editing an existing slot with the new item’s data?

So I assume there is no problem with that and your only concern is displaying the icon (sprite) of an item when it is awarded to the player. With that in mind,

Maybe you can change the function AddItem from a void return to an Item return. So when you add an item, instead of breaking, just return the new item. That way in your award function say this.

Item awardedItem = inv.AddItem(Random.Range(0,8));

//get the display that is supposed to show the item sprite.
itemDisplayImage.sprite = awardedItem.sprite;

And that will show the icon of whatever item you got.