Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to get a button to hide a gameobject

Discussion in 'Immediate Mode GUI (IMGUI)' started by Daniel101, Apr 10, 2014.

  1. Daniel101

    Daniel101

    Joined:
    Apr 10, 2014
    Posts:
    3
    First of all, im new to Unity and i cant program really, so please explain everything really good.
    Im unsure if i should post here or in the Scripting forum.

    What i want to happen are.
    When i press button "Up" it show the roof of my buildning.
    When i press button "Down" it hides the roof.

    My building is name "Project1-3DView-{3D}"
    And the roof is neasted inside the building and is called "Roof"

    This is my script as it looks now...
    Code (csharp):
    1. public class buttons : MonoBehaviour {
    2.  
    3.     public GUIStyle guiStyle;
    4.     // Use this for initialization
    5.     void Start () {
    6.    
    7.     }
    8.    
    9.     // Update is called once per frame
    10.     void Update () {
    11.    
    12.     }
    13.  
    14.     private void OnGUI() {
    15.         if (GUI.Button(new Rect(Screen.width - 200, Screen.height - 300, 120, 200), "Up", guiStyle))
    16.         {
    17.             Roof.SetActive(true);
    18.         };
    19.        
    20.         if (GUI.Button(new Rect(Screen.width - 200, Screen.height - 150, 120, 200), "Down", guiStyle))
    21.         {
    22.              Roof.SetActive(false);
    23.         };
    24.    
    25.     }
    26. }
    And that does of couse not work, but I have no idea how to call the roof..
     
    Last edited: Apr 11, 2014
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Why do you have a semicolon after the braces on lines 18 23? That isn't valid syntax so you'll definitely get errors. Also, you haven't declared the "Roof" variable so you ain't really referencing anything. First you should declare your variables, something like this should work :

    Code (csharp):
    1.  
    2.  
    3. public Transform Roof;
    4. public GUIStyle guiStyle;
    5.  
    6.  void OnGUI() {
    7.  
    8.         if (GUI.Button(new Rect(Screen.width - 200, Screen.height - 300, 120, 200), "Up", guiStyle))  {
    9.  
    10.             Roof.active = true;
    11.  
    12.         }
    13.  
    14.        
    15.  
    16.         if (GUI.Button(new Rect(Screen.width - 200, Screen.height - 150, 120, 200), "Down", guiStyle)){
    17.  
    18.              Roof.active  = false;
    19.  
    20.         }
    21.  }
    22.  
     
    Last edited: Apr 11, 2014
  3. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Actually the semicolons you mention are fine, they could cause an error if the second if was an else but since it is not the case, it works exactly the same. (just consider it an empty statement, and you could add as many as you wish even though there is no use to it).
     
  4. Daniel101

    Daniel101

    Joined:
    Apr 10, 2014
    Posts:
    3
    Like i said i cant code, so i just deleted the semicolons.

    So now the code looks like this
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class buttons : MonoBehaviour
    5. {
    6.  
    7.     public GameObject roof;
    8.     public GUIStyle guiStyle;
    9.  
    10.     void OnGUI()
    11.     {
    12.  
    13.         if (GUI.Button(new Rect(Screen.width - 160, Screen.height - 220, 150, 100), "Up", guiStyle))
    14.         {
    15.             roof.SetActive(true);
    16.         }
    17.         if (GUI.Button(new Rect(Screen.width - 160, Screen.height - 110, 150, 100), "Down", guiStyle))
    18.         {
    19.             roof.SetActive(false);
    20.         }
    21.     }
    22. }
    i got this warning when i used Transform and roof.active = true;
    So i changed the code to what you can see above.

    When i run the program now and press one of the buttons i get this error
    I can read what its telling me, but i dont know what to do to fix it...
     
  5. Daniel101

    Daniel101

    Joined:
    Apr 10, 2014
    Posts:
    3
    Oh i found the place to fix it in my inspector, now it works :D