- Home /
Please help I don't understand what is wrong.
Okay I am trying to make a GUI pop up after completing a set number of laps but I'm getting all these errors with the coding that I have put in saying to put something in instead of what I have and when I do what it is asking it just brings up even more errors. My code is in Javascript, I have a lot of things commented out because they worked and I wanted to keep them in so if I fail at adding this pop up I can have my original coding. If at all possible when explaining what I did wrong could you make it as thorough and simple as possible because I am extremely new to programming and can barely understand it Thank you very much for your help.
function OnTriggerEnter(other : Collider){
if (score <= 2){
score = score + 1;
print(score);
}
else
{
function OnGUI()
{
//(Application.LoadLevel("Tutorial Level"));
// Make a group on the center of the screen
GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 100));
// All rectangles are now adjusted to the group. (0,0) is the topleft corner of the group.
// We'll make a box so you can see where the group is on-screen.
GUI.Box (Rect (0,0,100,100), "Do you want to continue with the Tutorial or move on to the real tracks?");
if (GUI.Button (Rect (10,40,80,30), "Yes"))
{
Application.LoadLevel("Tutorial Level");
}
if (GUI.Button (Rect (30,60,100,50), "No"))
{
Application.LoadLevel("Title GUI");
}
// End the group we started above.
GUI.EndGroup ();
}
}
Please spend more time describing the nature of your question in the topic of the posting.
Answer by Nooch · May 08, 2012 at 02:52 AM
The OnGUI function shouldn't be inside OnTriggerEnter, it should be on it's own. OnGUI is a special built-in function, that is run multiple times every frame. So, it's constantly checking whether there are commands inside it to execute (this is also why it can be slow, when it is used too often).
To trigger the dialog, you could use a boolean (true / false) to tell OnGUI whether it should display your message box.
var showDialog : boolean; // can be set to true or false, to decide whether to show the UI
var score : int; // for the score
function OnTriggerEnter(other : Collider)
{
if (score <= 2)
{
score = score + 1;
print(score);
}
else
{
showDialog = true; // show the UI
}
}
function OnGUI()
{
if (showDialog) // if the showDialog boolean is true ...
{
GUI.BeginGroup (Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 100));
GUI.Box (Rect (0,0,100,100), "Do you want to continue with the Tutorial or move on to the real tracks?");
if (GUI.Button (Rect (10,40,80,30), "Yes"))
{
// load level
}
if (GUI.Button (Rect (30,60,100,50), "No"))
{
// load level
}
GUI.EndGroup();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613071223im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Hiding OnGUI? 1 Answer
[Solved] GUI Error? 1 Answer
How to be specific and universal with your onGUI controls? 1 Answer
Help! Why doesn't this work? GUI/Static vars Help! Javascript 1 Answer