onGUI triggering second function only
I have a script in which I press onGUI and it should change a static boolean to false, and load a new scene.
When running the script, whichever command is listed second will run on start-up, while the first works correctly and only triggers when pressing the GUI.
When running the same script with OnMouseDown it works as intended - Both events happen when the mouse is pressed on the relevant object.
When removing the secondary function, each one works in isolation with no issues.
Having hacked this together with little knowledge of what I'm doing, I was hoping someone could explain where I have tripped up. Thanks!
static var active : boolean = true;
var string = "Level name";
var customButton : GUIStyle;
function OnGUI () {
//the active script changes to false - this will prevent the score from updating anymore
if (GUI.RepeatButton(Rect (200,200,200,200), "", customButton))
active = false;
Application.LoadLevel(string);
}
Answer by EmHuynh · Feb 16, 2016 at 05:10 AM
Hello, @SuperBarrio!
The if statement does not have a set of curly brackets. So if its condition is true, the only thing that will happen is that active will be set to true.
Another problem is when you try to call Application.LoadLevel without the correct parameters. Also, Application.LoadLevel is obsolete. Use SceneManager.LoadScene instead. You can check it out here: http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html
If you have any more questions, let us know! (: