- Home /
??Whats wrong with this script??? HELP
I have a script that should:
On collision with a gameObject tagged 'Cube'
Display a GUI box with some text saying 'You got eaten by the cubes...'
And underneath that a GUI button with the text 'Restart Game'
When you press the GUI button, it restarts the current level.
Unity just comes up with lots of compiler errors. Can anyone debug this code and give me a version that is correct?
Thankyou in advance...
function OnCollisionEnter (hit : Collision)
{
if(hit.transform.gameObject.name == "Cube")
{
function OnGUI() {
GUI.Box (Rect (10, 10, 100, 20),"You got eaten by the cubes...");
if GUI.Button(Rect(10,70,50,30),"Restart Game")
Application.LoadLevel(Application.loadedLevel);
}
}
}
Answer by roojerry · Sep 02, 2013 at 12:39 PM
Take time to learn some basic programming, it would benefit you greatly. You have tried to declare the OnGUI function within the OnCollisionEnter function. You need to separate them into two functions and use a boolean to determine whether the GUI should show or not
function OnCollisionEnter (hit : Collision)
{
if(hit.transform.gameObject.name == "Cube")
{
wasEaten = true;
}
}
function OnGUI()
{
if(wasEaten)
{
GUI.Box (Rect (10, 10, 100, 20),"You got eaten by the cubes...");
if (GUI.Button(Rect(10,70,50,30),"Restart Game"))
{
Application.LoadLevel(Application.loadedLevel);
}
}
}
Thankyou for your answer. Looked right but when i tried it out it did't work:
expecting (, found GUI
unexpected token: .
(sigh) I've fixed the missing brackets....
You should really follow the advice of brianruggieri and learn the basics (the very basics).
Answer by f1a5b9i7e5n3 · Sep 02, 2013 at 12:51 PM
Maybe cause you have a function inside a function...
Your answer
Follow this Question
Related Questions
Can't add script - need to fix all compile errors 1 Answer
Why are these errors messing with me? 2 Answers
How to make an object spawn in different positions? 2 Answers
reseting rotation of objects? 0 Answers