Question by
sullivan2742 · Jan 29, 2019 at 08:18 PM ·
arrayinventorygameobjectstagscounter
Unity won't print last item in my array for my GameObjects
Hi, I created an array to find all game objects with the tag "ChoosenItems" when the player picks up 8 objects. It's supposed to print the name of the source image for that GameObject but for the last object it remains "empty_item". It prints out all elements of the array but it doesn't update the last item from "empty_item" to "CoinSprite". What is the problem?
Here is the code. using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PickUpItem : MonoBehaviour, IInteractable {
public string DisplaySprite;
public string DisplayImage;
public static int counter;
public static GameObject InventorySlots;
public static GameObject[] PlayerItems = new GameObject[8];
public void Interact(DisplayImage currentDisplay)
{
ItemPickUp();
}
void Start()
{
}
void Update()
{
}
public void ItemPickUp()
{
InventorySlots = GameObject.Find("Slots");
int j;
counter = 0;
foreach (Transform slot in InventorySlots.transform)
{
if (slot.transform.GetChild(0).GetComponent<Image>().sprite.name == "empty_item")
{
slot.transform.GetChild(0).GetComponent<Image>().sprite =
Resources.Load<Sprite>("Inventory Items/" + DisplaySprite);
Destroy(gameObject);
break;
}
if (counter <= 7)
{
counter++;
if (counter >= 7)
{
{
//https://hub.packtpub.com/arrays-lists-dictionaries-unity-3d-game-development/
Debug.Log("You have choosen all your items.");
PlayerItems = GameObject.FindGameObjectsWithTag("ChoosenItem");
for (j = 0; j < 8; j++)
{
Debug.LogFormat("Item[{0}] = {1}", j, PlayerItems[j].GetComponent<Image>().sprite.name);
}
}
}
}
}
}
}
Comment