Hi there, I am trying to work out how to attached functions to each state of the boolean (by GUI.toggle) : Code (csharp): #pragma strict var paused : boolean = false; function Update () { if (paused) { print ("button on"); } if (!paused) { print ("button off"); } } function OnGUI () { paused = GUI.Toggle (Rect (0,400,60,20), paused, "label"); } 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?
Use GUI.changed. Code (csharp): function OnGUI() { paused = GUI.Toggle(Rect(0, 400, 60, 20), paused, "label"); if (GUI.changed) { if (paused) Debug.Log("Button On"); else Debug.Log("Button Off"); } }
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): private bool bSimple = true; public bool Simple { get { Debug.Log("Simple Retreived"); return bSimple;} set { bSimple = value; Debug.Log("Simple Toggled"); } You could do this similarly with a function thought Code (csharp): private bool bSimple = true; public bool getSimple() { return bSimple;} public void setSimple(bool value) { Debug.Log("Simple Set"); bSimple = value;} This is C# though, but i think the same idea applied here too.
Good, but I can't add any more code to each state, throws up an error, such as time scale: Code (csharp): if (GUI.changed) { if (paused) Debug.Log("Button On"); Time.timeScale = 0; else Debug.Log("Button Off"); Time.timeScale = 1; } 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): var paused : boolean = false; function Update() { if(Input.GetKeyDown("escape") !paused) { print("Paused"); Time.timeScale = 0; } else if(Input.GetKeyDown("escape") paused) { print("Unpaused"); Time.timeScale = 1; } } So cloning the functionality of the hotkey toggle and the GUI toggle.. I must be close...
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): var guiEnabled : boolean = false; function Update() { if(input.GetKeyDown("Up Arrow") { guiEnabled = true; } if(input.GetKeyDown("Down Arrow") { guiEnabled = false; } } function OnGUI() { if(GUIenabled == true; { GUI.Button (Rect (0,400,60,20), paused, "label"); } } This should make a button appear when you press the up arrow key, and make it disappear when you press the down arrow key
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): if (GUI.changed) { if (paused) { Debug.Log("Button On"); Time.timeScale = 0; } else { Debug.Log("Button Off"); Time.timeScale = 1; } }
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.