1

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);
        }
        }

    }
3
  • you cant do findgameobjectswithtags to get prefabs, unless they are already instanced. If you need actual prefabs it sounds more like you should be looking at scriptable objects Commented Feb 29, 2024 at 17:12
  • The prefab clones get assigned the tag name through a Script. This works fine is it still not possible to use findgameobjectswithtags if the prefabs get assigned a tag after the are spawnedin the game? Commented Mar 1, 2024 at 14:25
  • as I said, if they are instanced you can find the instances, but not the prefabs Commented Mar 1, 2024 at 15:18

0

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.