- Home /
The question is answered, right answer was accepted
BCE0044: expecting (, found 'OnGUI'.
Hello,Im trying to make a "game over,restart" menu popup when a 2D collider enters a trigger.At first I made a scene where it shows you a 'game over' text and you had to press the retry button to restart and it worked great.Now when I try to make some buttons show up,it gives me these errors :
"Assets/Scripts/Destroyer.js(15,10): BCE0044: expecting (, found 'OnGUI'."
And this one :
"Assets/Scripts/Destroyer.js(15,17): UCE0001: ';' expected. Insert a semicolon at the end."
This is the script im using :
#pragma strict
public var other : GameObject;
var myGUISkin : GUISkin;
function OnTriggerEnter2D(other : Collider2D)
{
if(other.tag == "blackballoon")
{
Destroy (other.gameObject);
}
if(other.gameObject.tag == "balloon")
{
function OnGUI(){
GUI.skin = myGUISkin;
if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2+3,200,50),"Restart")){
Application.LoadLevel(Application.loadedLevel);
}
}
}
}
Thnak you for your time.
Answer by robertbu · Jun 19, 2014 at 03:26 PM
You need to take the time to indent and match your braces. Pick a single bracketing style, not the mix and match as you've done here. Once you have your bracketing fixed, you will see that your OnGUI() function is nested inside your OnTriggerEnter2D function. You cannot nest functions. Pulling OnGUI() out to the top level will fix this error.
What exactly do you mean by pulling OnGUI() on top,if I do then wouldn't the function be called before the object enters the 2d trigger ?
If you want to prevent GUI object from showing until the object enters the trigger, you must use a boolean flag.
#pragma strict
public var other : GameObject;
var myGUISkin : GUISkin;
var showGUI = false;
function OnTriggerEnter2D(other : Collider2D) {
if(other.tag == "blackballoon") {
Destroy (other.gameObject);
}
if(other.gameObject.tag == "balloon") {
showGUI = true;
}
}
function OnGUI(){
if (showGUI) {
GUI.skin = myGUISkin;
if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2+3,200,50),"Restart")){
Application.LoadLevel(Application.loadedLevel);
}
}
}
Again, you cannot nest functions. OnGUI() is a callback that gets called every frame, so you cannot call OnGUI() from inside OnTriggerEnter2D() either.
Follow this Question
Related Questions
Performance Optimization ~Function Update: Loop or Once ? 5 Answers
Disable function OnGUI after 4 seconds 1 Answer
GUI opacity. 1 Answer
Functions overwrite or work seperatelly ? 1 Answer
javascript equivalent of Action? 0 Answers