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? :slight_smile:

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

5666137--590095--upload_2020-4-3_16-21-16.png

This is the only thing i see in the inspector…

Hah.

YOur code isn’t drawing any buttons.

When OnGUI is called, you’re supposed to call functions that actually spawn buttons from there.

See example here:
https://docs.unity3d.com/ScriptReference/GUILayout.Button.html

Also read here:
https://docs.unity3d.com/Manual/editor-CustomEditors.html

See the checkbox next to the name of the object? If you uncheck it the object will start off disabled.

Be aware that this method doesn’t have anything to do with the current UI (the one where you place components within the Scene View). It’s intended to be used to create custom editors for the Inspector.

1 Like

That is correct. THis method is supposed to be used for creating custom inspectors for objects.

Normally to have buttons do things during gameplay, you use more modern UI which uses canvas and button objects.

Hmmmm I don’t really see what you mean… That’s the button for picking a GameObject…
disloyalsentimentallangur