I am currently unable to print all elements of my array. I am trying to use a nested for loop to print and number all elements but I can only print one element over and over again which is the last element that I click in the game.
The first 7 elements printed are all the same and the last element is left completely blank.
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 int counter;
public int j;
public string[] ChoosenItems = new string[7];
private GameObject InventorySlots;
public void Interact(DisplayImage currentDisplay)
{
ItemPickUp();
}
void Start()
{
}
void Update()
{
}
void ItemPickUp()
{
InventorySlots = GameObject.Find("Slots");
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)
{
ChoosenItems[counter] = (gameObject.name);
counter++;
if (counter >= 7)
{
Debug.Log("You have choosen 8 itmes, would you like to continue or retry?");
for (j = 0; j <= 7; j++)
{
Debug.LogFormat("Element[{0}] = {1}", j, ChoosenItems[j]);
}
}
}
}
}
}
//http://csharp.net-informations.com/collection/list.html