How can i Assign all prefabs with a certain Tag to a List in Unity?

28 views Asked by At

Im developing a Game in Unity and currently im having a hard time making the inventory System Work. Im a beginner in Unity so the problem is possiblie something obvious to some of you.

The inventory contains a list of Toggles on the left. Each Toggle is the Name of a Item. According to the Selected Toggel a fitting Item Description of the Selected Item appers on the Right side of the Screen. The Items are Collected through the interaction with a Scriptabel Object named Item which contains a number of Prefabs including ItemName ItemDescription and a Tag string. InventarManager Script loads the Prefabs into the Inventory Canvas after the Item is Collected. the Collecting of Items and adding of the Prefabs to the Inventory works fine. The Problem is that i cant make the Toggle Work.

Both the Toggle **ItemName **and the description **ItemDescription **are all Prefabs. So i have to set up the Toggle funktions via Script. I want the ItemName Toggle to deactivate all unselected ItemDescription Prefabs and Activate the Selected **ItemDescription ** note that i also want to add a ItemModel that behaves equal to ItemDescription for each Item after this system works.

I collected all Possible Item Tags in the list itemTags they are all Already defined. **SelectedTag **is a String variable with the fitting tag the toggle should influence. It is drawn from the Item data and is correctly transportet to my Script. now i want to get all Prefabs with the **SelectedTag **and assign them to **ListA **and all Prefabs with another Tag from itemTags and assing them to ListB. The toggle should later deactiveate **ListB **and Activate **ListA **if it is selected. but the adding to the lists already doesnt work. Can you guys help me?

   using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
   using UnityEngine.UI;
   public class InventoryToggle : MonoBehaviour
   {
   public Toggle toggle;

   [SerializeField]
   //a list with all Prefabs including ItemDescriptions and ItemModel for the currently Selected ItemName          Toggle
   private List<GameObject> SelectedItem = new List<GameObject>();

   [SerializeField]
   //a list with all Prefabs including ItemDescriptions and ItemModel for the currently Unselected    ItemName Toggles
   private List<GameObject> OtherItem = new List<GameObject>();

   //a list with all Possible Tags for all Items. Each Item has its own already defined Tag
   public string[] itemTags = new string[] {"ItemTabletten","ItemPostkarte"};

   //This Method SHOULD look for all of the Prefabs with the Selectedtag and assignes the Game Objects to    SelectedItem. It also assignes all Game Object with aTag != ItemTag to OtherObjects  
   public void AssignToggle(string Selectedtag)
    {


        GameObject[] selectedobjects = GameObject.FindGameObjectsWithTag(Selectedtag);
        SelectedItem.AddRange(selectedobjects);

        foreach (string s in itemTags)
        {
          if (s != Selectedtag)
        {
        GameObject[] otherobjects = GameObject.FindGameObjectsWithTag(s);
        OtherItem.AddRange(otherobjects);
        }
        }

    }
0

There are 0 answers