- Home /
Setting GameObjects inactive/invisible by default in GUI?
Hello. I made a GUI with some GameObjects, but they are active, or in other words, they are visible when i start up the game. I want them to be invisible by default when i start up the game, but i cannot figure out where to do that in the code? Any help? :)
Here is my code
public GameObject[] objects;
private void OnGUI()
{
foreach (GameObject go in objects)
{
bool active = GUILayout.Toggle(go.activeSelf, go.name);
if (active != go.activeSelf)
{
go.SetActive(active);
}
}
}
Hi@mathhiasfri if you want to toggle the value you can do this
//you can set object activeself to false in the begining or in the
//inspector by untick gameobject's text boxes or in Awake
void Awake()
{
foreach (GameObject go in objects)
{
go.SetActive(false);
}
}
private void OnGUI()
{
foreach (GameObject go in objects)
{
bool active = GUILayout.Toggle(!go.activeSelf, go.name);
}
}
Answer by Bunny83 · Apr 04, 2020 at 11:34 AM
You mean something like this?
// [...]
public GameObject[] objects;
private void Start()
{
foreach (GameObject go in objects)
go.SetActive(false);
}
private void OnGUI()
{
// [...]
Sadly no. This just disables them completely as i press them in the GUI. I still want the player to be able to make it invisible and visible whenever they want. I just want the objects to start off being invisible.
Answer by promant12 · Apr 04, 2020 at 11:58 AM
You can just disable them in the Editor.
Where.... I've heard it is the box in the gif below, but that is just a button for choosing a GameObject.?