Search Unity

boolean functions

Discussion in 'Scripting' started by GizmoBradwell, Jun 18, 2012.

  1. GizmoBradwell

    GizmoBradwell

    Joined:
    Dec 27, 2010
    Posts:
    67
    Hi there,


    I am trying to work out how to attached functions to each state of the boolean (by GUI.toggle) :

    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4.  
    5.  
    6.  
    7. var paused : boolean = false;
    8.  
    9.  
    10. function Update () {
    11.  
    12.  
    13.  if (paused) {
    14.   print ("button on");
    15.  }
    16.  if
    17. (!paused) {
    18.   print ("button off");
    19.  }
    20. }
    21.  
    22.  
    23.  
    24. function OnGUI () {
    25.  
    26. paused = GUI.Toggle (Rect (0,400,60,20), paused, "label");
    27.  
    28.  
    29.  
    30. }
    31.  
    Can you see what I mean? This prints the message every frame I know, so how do I fix that to print when I hit the button, and how do I trigger a bunch of stuff when I hit the button and then another bunch of stuff when I toggle it off again?
     
  2. ModStoryGames

    ModStoryGames

    Joined:
    Apr 27, 2012
    Posts:
    179
    Use GUI.changed.

    Code (csharp):
    1.  
    2. function OnGUI()
    3. {
    4.    paused = GUI.Toggle(Rect(0, 400, 60, 20), paused, "label");
    5.    if (GUI.changed)
    6.    {
    7.       if (paused)
    8.          Debug.Log("Button On");
    9.       else
    10.          Debug.Log("Button Off");
    11.    }
    12. }
    13.  
     
  3. szi_John

    szi_John

    Joined:
    Feb 10, 2012
    Posts:
    70
    i don't know what the equivalent to this is in unity script / java script but it sounds like a good use of properties from C#

    Code (csharp):
    1.  
    2. private bool bSimple = true;
    3. public bool Simple
    4. {
    5.     get { Debug.Log("Simple Retreived"); return bSimple;}
    6.     set { bSimple = value; Debug.Log("Simple Toggled");
    7. }
    8.  
    You could do this similarly with a function thought
    Code (csharp):
    1.  
    2. private bool bSimple = true;
    3. public bool getSimple() { return bSimple;}
    4. public void setSimple(bool value) { Debug.Log("Simple Set"); bSimple = value;}
    5.  
    This is C# though, but i think the same idea applied here too.
     
  4. GizmoBradwell

    GizmoBradwell

    Joined:
    Dec 27, 2010
    Posts:
    67
    Good, but I can't add any more code to each state, throws up an error, such as time scale:

    Code (csharp):
    1.  
    2.     if (GUI.changed) {
    3.     if (paused)
    4.       Debug.Log("Button On");
    5.       Time.timeScale = 0;
    6.     else
    7.       Debug.Log("Button Off");
    8.       Time.timeScale = 1;
    9.    }
    10.  
    11.  
    That's why at first I had them in an Update function. So just wanted to know how to write an if statement for the pause boolean true or false, as i'm also wanting this toggle button to be triggerd by hotkey:

    Code (csharp):
    1.  
    2. var paused : boolean = false;
    3. function Update() {
    4.      if(Input.GetKeyDown("escape")  !paused)
    5.    {
    6.       print("Paused");
    7.       Time.timeScale = 0;
    8.    }
    9.    else if(Input.GetKeyDown("escape")  paused)
    10.    {
    11.       print("Unpaused");
    12.       Time.timeScale = 1;
    13.    }
    14. }
    15.  
    16.  
    So cloning the functionality of the hotkey toggle and the GUI toggle.. I must be close... :D
     
  5. Code_Of_Honour

    Code_Of_Honour

    Joined:
    May 28, 2012
    Posts:
    293
    I don't know if you're trying to toggle GUI buttons, or if you're trying something else completely different...but shouldn't this work?

    Code (csharp):
    1.  
    2. var guiEnabled : boolean = false;
    3.  
    4. function Update()
    5. {
    6.       if(input.GetKeyDown("Up Arrow")
    7.       {
    8.             guiEnabled = true;
    9.       }
    10.       if(input.GetKeyDown("Down Arrow")
    11.       {
    12.            guiEnabled = false;
    13.       }
    14. }
    15.  
    16. function OnGUI()
    17. {
    18.       if(GUIenabled == true;
    19.       {
    20.              GUI.Button (Rect (0,400,60,20), paused, "label");
    21.       }
    22. }
    This should make a button appear when you press the up arrow key, and make it disappear when you press the down arrow key :D
     
    Last edited: Jun 19, 2012
  6. apparition

    apparition

    Joined:
    Jan 11, 2012
    Posts:
    120
    Be careful not to forget the { } when you have more than one line in an if or else block. Without the curly brackets only the first line is included in the block.

    Code (csharp):
    1.  
    2.     if (GUI.changed) {
    3.       if (paused)
    4.       {
    5.         Debug.Log("Button On");
    6.         Time.timeScale = 0;
    7.       }
    8.       else
    9.       {
    10.         Debug.Log("Button Off");
    11.         Time.timeScale = 1;
    12.       }
    13.    }
    14.  
     
    Last edited: Jun 19, 2012
  7. GizmoBradwell

    GizmoBradwell

    Joined:
    Dec 27, 2010
    Posts:
    67
    Thanks! I wondered what I missed... I thought it was because it wasn't in an Update function, but was confused because the onGUI is like the Update function anyway.