Problem with give up -button
Hi, this is my first project in Unity and I'm making a simple 3D platformer. (I'm on a course where we learn the basics of making 3D and 2D games in Unity) Every time you finish/fail a level, you see a screen that says "Level Completed!", "Continue" or "Level Failed!", "Restart". I'm trying to give the player the option to give up but I can't get it working. We were given the following script:
void OnGUI() {
GUI.skin.label.fontSize = 20;
GUI.skin.label.alignment = TextAnchor.MiddleLeft;
if (levelComplete) {
if (LevelEndMessage("Level completed!", "Continue"))
{
int nextLevel = Application.loadedLevel + 1;
if (nextLevel >= Application.levelCount) nextLevel = 0;
Application.LoadLevel(nextLevel);
}
}
else if (levelFailed) {
if (LevelEndMessage("You tried.", "Keep trying"))
Application.LoadLevel(Application.loadedLevel);
if (GUI.Button(new Rect(Screen.width/2 - 200/2, Screen.height/2 + 100, 200, 50) "Give up")) Application.LoadLevel(0);
}
else {
GUI.skin.label.alignment = TextAnchor.MiddleLeft;
GUI.Label(new Rect(10, 10, 200, 30), "Time:" + (Time.time - startTime).ToString("#"));
GUI.Label(new Rect(10, 40, 200, 30), "Collected:" + collected + "/" + numTreasures);
}
}
and I can't get it working. If I switch the
else if (levelFailed) {
if (LevelEndMessage("You tried.", "Keep trying"))
Application.LoadLevel(Application.loadedLevel);
if (GUI.Button(new Rect(Screen.width/2 - 200/2, Screen.height/2 + 100, 200, 50) "Give up")) Application.LoadLevel(0);
for
else if (player == null) {
//changed that ^
if (LevelEndMessage("You tried.", "Keep trying"))
Application.LoadLevel(Application.loadedLevel);
//if (GUI.Button(new Rect(Screen.width/2 - 200/2, Screen.height/2 //+ 100, 200, 50) "Give up")) Application.LoadLevel(0);
//remove that ^
I can play the game and restart after death, but I can't give up. I'm a beginner (at best) at coding so I don't know if this is enough information for troubleshooting. The game won't run and it gives the following errors for the line that I removed: "A new expression requires () or [] after type" and "Internal compiler error during parsing, Run with -v for details".
Thank you and sorry if this is a common rookie mistake that I should be able to fix on my own.
Answer by IsaiahKelly · Aug 21, 2016 at 06:12 PM
Looks like you're missing a comma (,) right before the "Give up" string in your button code.
Tip: Double click on any error message in the console to be taken directly to the line of code in your script that's causing the issue. You should then see a red line under the part of code that's causing the problem, and hovering over that part should give you additional info.
Answer by Tamms · Aug 22, 2016 at 05:05 AM
Thank you, it was the missing comma and thanks for the tip!
Your answer
Follow this Question
Related Questions
Pause menu Change 0 Answers
Writing dropdown selections to file 1 Answer
Open Menu After Animation 1 Answer
How to use this menu (skins) in my game? 0 Answers
Responsive UI 0 Answers