1

I need some help. I have this functional script to enable/disable gameobjects with a dropdown:

public class DROPDOWNOPT : MonoBehaviour
 {
 
     public Dropdown MenuDesplegable;
     public GameObject GameObject1;
     public GameObject GameObject2;
     public GameObject GameObject3;
 
      void Update()
     {
         switch(MenuDesplegable.value)
         {
             case 0:
                 GameObject1.SetActive(false);
                 GameObject2.SetActive(false);
                 GameObject3.SetActive(false);
                 break;
 
             case 1:
                 GameObject1.SetActive(true);
                 GameObject2.SetActive(false);
                 GameObject3.SetActive(false);
                 break;
 
             case 2:
                 GameObject2.SetActive(true);
                 GameObject1.SetActive(false);
                 GameObject3.SetActive(false);
 
                 break;
 
             case 3:
                 GameObject3.SetActive(true);
                 GameObject2.SetActive(false);
                 GameObject1.SetActive(false);
 
                 break;
 
         }
 
     }
 
 }

but that script only work with a specific quantity of gameobject. I need to create a list of gameobjects in the inspector where I can add/attach the desired quantity of them.

there is my attempt:

 namespace Assets
 {
     class DynamicListOBJ : MonoBehaviour
     {
         public Dropdown MenuDesplegable;
 
         [SerializeField]
         private GameObject[] dynamicList;
         private GameObject[] instanciatedObjects;
 
         private void Start()
         {
             Fill();
         }
 
         public void Fill()
         {
             instanciatedObjects = new GameObject[dynamicList.Length];
             for (int i = 0; i < dynamicList.Length; i++)
             {
                 instanciatedObjects[i] = Instantiate(dynamicList[i]) as GameObject;
             }
         }
 
         private void Update()
         {
             switch (MenuDesplegable.value)
             {
                 case 0:
                     instanciatedObjects[i1].SetActive(true);
                     instanciatedObjects[i2].SetActive(false);
                     instanciatedObjects[i3].SetActive(false);
                     break;
                 case 1:
                     instanciatedObjects[i1].SetActive(false);
                     instanciatedObjects[i2].SetActive(true);
                     instanciatedObjects[i3].SetActive(false);
                     break;
                 case 3:
                     instanciatedObjects[i1].SetActive(false);
                     instanciatedObjects[i2].SetActive(false);
                     instanciatedObjects[i3].SetActive(true);
                     break;
 
             }
 
         }
 
     }
 
 }

my problem is with the content in the update function. I dont know how reference and enable/disable the gameobjects from the list.

can give me some guidance?. PD: I only need one gameobject active at the same time. PD1: in the start all gameobjects are inactive.

solved, solution:

using UnityEngine;
using UnityEngine.UI;

namespace Assets
{
    class DynamicListOBJ : MonoBehaviour
    {
        public Dropdown MenuDesplegable;
        //public GameObject ContentContainer; //used to set where the instantiated objects are added.

        [SerializeField]
        private GameObject[] dynamicList;
        private GameObject[] instantiatedObjects;

        private void Start()
        {
            Fill();
        }

        public void Fill()
        {
            instantiatedObjects = new GameObject[dynamicList.Length];
            for (int i = 0; i < dynamicList.Length; i++)
            {
                instantiatedObjects[i] = (dynamicList[i]) as GameObject;
                //instantiatedObjects[i] = Instantiate(dynamicList[i]) as GameObject;  //this create a instantiate clone
                //instantiatedObjects[i].transform.parent = ContentContainer.transform;
                //instantiatedObjects[i].transform.localScale = dynamicList[i].transform.localScale;
            }
        }

        void DisableObjects()
        {
            foreach (GameObject o in instantiatedObjects)
            {
                o.SetActive(false);
            }
        }
        public void DropdownValueChanged(int value)
        {
            DisableObjects();
            instantiatedObjects[value].SetActive(true);
        }
     
    }

}
2
  • You reference them as you have done like an array. Just for loop over them Commented Sep 20, 2020 at 1:54
  • Hello, can you explain me a little more?, I create the first script by myself, but the second one I followed some examples but I don't understand completely how to create the syntaxis Commented Sep 20, 2020 at 2:49

1 Answer 1

2

First of all you dont need to do that in Update since it will only change when Dropdown value changed. So first we will add our function to Dropdown in Unity and select Dynamic int (Onvaluechanged you can see it when you select the Dropdown in Unity).

Then you can use the value like that:

void DisableObjects()
{
    foreach (GameObject o in instanciatedObjects)
    {
        o.SetActive(false);
    }
}
public void DropdownValueChanged(int value)
{
    DisableObjects();
    instanciatedObjects[value].SetActive(true);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hello, Thanks for answer. I added your two functions, I changed the: instanciatedObjects[index].SetActive(true); to instanciatedObjects[value].SetActive(true); it seems to work (the instantiated objects are enabling/disabling). but I still have a problem with my Fill() function. the "clone" gameobject it creates, are created outside of my panel container, so they not are displayed in screen.
You have to give coordinate when you instantiate. Like Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity); [docs.unity3d.com/ScriptReference/Object.Instantiate.html]
thanks, solved now, I added your solution to the main post.

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.