What I want to do is something like this:
public GameObject obj1;
if (obj1 is assigned)
{
public GameObject obj2;
}
if (obj2 is assigned)
{
public GameObject obj3;
}
… and so on…
What I want to do is something like this:
public GameObject obj1;
if (obj1 is assigned)
{
public GameObject obj2;
}
if (obj2 is assigned)
{
public GameObject obj3;
}
… and so on…
Use an array or List instead. You can’t dynamically add or remove variables.
using System.Collections.Generic;
public class YourClass : MonoBehaviour
{
public List<GameObject> objs = new List<GameObject>();
}
you can use objs.Add(someGameObject);
to add another object to the end of the list. In the inspector in Unity you can simply increase the count to have “more slots”.
Don’t you just want:
public GameObject[] obj;
?
If you want to INSTANTIATE this game object in the game, you should use instantiate(object, position, rotation) method.
public GameObject[] myObjects;
public int currentObject = 0;
void intantiateObject(){
GameObject newGameObject = (GameObject)Instantiate(myObjects[currentObject], Vector3.zero, Quaternion.identity);
// The instantiation will make the game object visible if the object added to the array is active, if don't then you can use:
newGameObject.SetActive(true);
currentObject++;
}
If you just want to show and hide them, then use an array reference to the objects.
public GameObject[] myObjects;
public int currentObject = 0;
void intantiateObject(){
myObjects[currentObject].SetActive(true);
currentObject++;
}
I hope this help you.