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