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.SetParent(slots.transform);
itemObj.transform.position = Vector2.zero;
itemObj.GetComponent().sprite = itemToAdd.Sprite;
itemObj.name = itemToAdd.Title;
break;
}
}
}
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