Hello,
I’m trying to prototype an inventory and having an issue updating my UI Text. All I want to achieve for now is to list my inventory down the side of the screen and allow the user to scroll through the inventory which will add a “>” to the item’s text field.
I’ve managed to do most of this. When the user picks up items in the scene, the UI refreshes and draws them to the screen. Pressing Q after having both items in the inventory draws a “>” in front of the first picked up item. Pressing Q again should remove the “>” from the first item and add it to the second. However, both items get the “>”. Pressing Q a third time correctly removes “>” from both items as I now have nothing selected.
I tried calling the method that I use to redraw the inventory (thus grabbing their original names and removing the “>” like I do when no items are selected) but then no “>” draw at all despite me debugging and can see that the object I am trying to effect seems correct and the .text seems to be set correctly.
I’m confused my adding this method call UpdateItemInventory() in UpdateItemSelection() makes my Text components not update at all.
Hopefully I’ve explained this well enough, any help would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour {
private GameObject player;
// Temp variables for placeholder UI
public GameObject tempInventoryText;
private Transform tempInventoryUI;
public int itemSelected = 0;
// Create an array to store picked up items
private static List<GameObject> inventoryItems = new List<GameObject>();
void Start()
{
player = gameObject.GetPlayer();
// Temp UI gameObject has a tag UI_Parent. This has a vertical layout so each Text script added to it when
// the UI is updated gets put into a new row.
tempInventoryUI = GameObject.FindGameObjectWithTag("UI_Parent").transform;
}
void Update()
{
if (Input.GetButtonDown("ItemSelect"))
{
if(inventoryItems.Count > 0)
{
if (itemSelected < inventoryItems.Count)
{
itemSelected++;
UpdateItemSelection();
}
else
{
itemSelected = 0;
UpdateItemSelection();
}
}
}
}
// Method that allows gameobjects to be added to the players inventory, can be called from other scripts.
public void AddItemToInventory(GameObject itemObject)
{
inventoryItems.Add(itemObject);
print("The player has " + inventoryItems.Count + " items in their inventory!");
int i = 0;
foreach(GameObject inventoryItem in inventoryItems)
{
print("Inventory item [" + i + "] is : " + inventoryItem);
i++;
}
// Once items have been added to the inventory, update the temp UI and update selection
UpdateItemInventory();
UpdateItemSelection();
}
private void UpdateItemInventory()
{
// Delete all inventory from the screen before replacing it.
// For every transform under UI_Parent, destroy.
foreach(Transform trans in tempInventoryUI)
{
Destroy(trans.gameObject);
}
// For each item in the inventory, instanciate empty text prefab and put it under the tagged GameObject, "UI_Parent"
foreach(GameObject inventoryItem in inventoryItems)
{
GameObject tempItemUI = Instantiate(tempInventoryText, tempInventoryUI) as GameObject;
Text tempUIText = tempItemUI.GetComponentInChildren<Text>();
// Set the text of the empty Text prefab to the current for each loop item name.
tempUIText.text = inventoryItem.name;
}
}
private void UpdateItemSelection()
{
if (inventoryItems.Count > 1)
{
if (itemSelected != 0)
{
//Leaving the below call in makes no ">" draw to the screen at all. Taking away the method allows me to draw them to the screen but
// I need to try and clear the UI before adding them so that I don't get all items with a ">"
UpdateItemInventory();
Text tempItemSelected = tempInventoryUI.GetChild(itemSelected - 1).gameObject.GetComponentInChildren<Text>();
string currentItemUIText = tempItemSelected.text;
tempItemSelected.text = ">" + currentItemUIText;
}
else
{
// Redraw inventory
UpdateItemInventory();
}
}
}
}