Search Unity

  1. Click here to receive a gift with your purchase of Unity Pro or Unity Enterprise.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Setting GameObjects inactive/invisible by default in GUI?

Discussion in 'Editor & General Support' started by mathiasfri, Apr 3, 2020.

  1. mathiasfri

    mathiasfri

    Joined:
    Mar 22, 2020
    Posts:
    3
    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

    Code (CSharp):
    1.     public GameObject[] objects;
    2.  
    3.     private void OnGUI()
    4.     {
    5.         foreach (GameObject go in objects)
    6.         {
    7.             bool active = GUILayout.Toggle(go.activeSelf, go.name);
    8.             if (active != go.activeSelf)
    9.             {
    10.                 go.SetActive(active);
    11.             }
    12.         }
    13.     }
     
  2. mathiasfri

    mathiasfri

    Joined:
    Mar 22, 2020
    Posts:
    3
    upload_2020-4-3_16-21-16.png

    This is the only thing i see in the inspector..
     
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    8,447
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    16,254
    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.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    8,447
    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.
     
  6. mathiasfri

    mathiasfri

    Joined:
    Mar 22, 2020
    Posts:
    3

    Hmmmm I don't really see what you mean... That's the button for picking a GameObject...
     
unityunity