Hello guys, I am trying to make a very simple Inventory based on text.
I have an issue where for the foreach loop only prints out the last item in the loop to the UI text.
The script I have wrote is simple, I just can’t see the issue. It may be better to use a list or a dict but I wanted to try out an array first.
public Text inventory;
public string[ ] backpack;
public void Update() {
if (Input.GetKey (KeyCode.B)) {
foreach (string items in backpack) {
inventory.text = items;
}
} else {
inventory.text = " ";
}
}
All it is doing, (as if you can’t already figure that out) is creating a public string array, I have added 5 items to the array back in unity. The foreach loop should loop over each item and print out all of the items in the UI text called inventory. It does work but it only prints out the last item in the array.
I also am unsure how would I add line breaks between each item? So it is not a long sentence type output.
Thank you guys