loop through array when active bool

Hi,

i have an array with all items that i can pick up in game. The code scrolls through all items in the array when i press a key. But i want to scroll only through items that are actually picked up.

Maybe with a second array for bools. That are set true, when Playerprefs are greater than 0 like this:

if (PlayerPrefs.GetInt(“Fruit”) > 0)
{

activeBool[1] = true;
}
But how can i combine it?

public GameObject[] items;



if (Input.GetKeyDown(KeyCode.O))
        {
            if (currentActive == items.Length - 1) currentActive = 0;
            else currentActive++;

            for (int i = 0; i < items.Length; i++)
            {
               
                    if (i == currentActive) items[i].SetActive(true);
                    else items[i].SetActive(false);
               
            }
        }

Simply make a second list that holds the collected items. Here i used a HashSet but you can also use other collections like List:

public GameObject[] items;
HashSet<GameObject> pickedItems  = new HashSet<GameObject>();
 
 
   public void Start()
    {
        // Pick up first 3 items for test
        pickedItems.Add(items[0]);
        pickedItems.Add(items[1]);
        pickedItems.Add(items[2]);
    }    


   public void LoopAllPickedItems()
   {

     foreach (GameObject item in pickedItems)
       {
            Debug.Log("Collected item: " + item.name);
       }
   }

  void Update()
    {
       if (Input.GetKeyDown(KeyCode.P))  // List all collected items if you press P
       {
           LoopAllPickedItems();
       }
    }
1 Like