0

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
2
  • Have you tried to debug your code? Commented Jan 26, 2019 at 19:24
  • I have, when my counter is under 7, it adds an item to the array and adds 1 to the counter. It then adds that item to the array over and over again because that is what it is told to do. Is there another way to perform my function without a for each loop? The break from the for loop only allows it to run once so I can't add to the counter or array in it. Commented Jan 27, 2019 at 13:45

1 Answer 1

2

You are declaring your ChoosenItems = new string[7] with 7 element, ChoosenItems[0], through ChoosenItems[6]. As you can see, this is only 7 items. Thus, you will never have 8 items. Try declaring it as ChoosenItems = new string[8] to have it contain 8 items. See Mozilla Developer Docs for more information.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.