I have created a game with 10 levels where if the player dies, a “Restart” scene pops up. The user can either select Restart or Quit. If user selects Restart, I need the previous level to load. If user passes level 1 and makes it to level 2 and dies, on the Restart scene, I would like user to start over on level 2, and etc.
What error did you get? Exactly how didn’t it work, basically. I’m not sure if you meant it loaded the wrong level, gave an error, or didn’t do anything (which means that there may be a problem with your code, I suggest using something like Debug.log(“Code went through here”) to check if it’s really processed).
You can also try Application.LoadLevel(Application.loadedLevelName), although you may want to double check the docs to make sure I got the right spelling
Also, it may be that Application.loadedLevel doesn’t work as the scene(s) aren’t added to your build settings, and thus they don’t actually have a number to load from.
using UnityEngine;
using System.Collections;
public class Restart : MonoBehaviour
{
void OnGUI()
{
if (GUI.Button (new Rect (250, 300, 100, 30), “Restart”))
{
Application.LoadLevel (Application.loadedLevel);
}
if (GUI.Button (new Rect (250, 400, 100, 30), “Quit”))
{
Application.Quit();
}
}
}
Erm… Now I’m confused due to the “” ironically… Is that a honest face that says it worked, or is a sadistic face that expresses your hatred at the issue on hand?
So how did Debug.log (or print) go?
Edit: Also could just mean that you’re happy that you tried it already. Possible, likely, not going to assume it.
That would be a sadistic face that expresses my anger on the issue at hand about the Application.LoadLevel, ha ha. In the Debug.Log (“Nothing happened”) when Restart button is clicked, shows up.
I’m going to just guess that this is an issue with how Application methods work and the whole Unity function process (like OnGUI is called pretty much at the end). My suggestion: (which is how I’ve always done it) make a function called “Restart()” and just call on that. Should work like a charm… hopefully.
Other than that, it should work. Unity API is quite interesting, although it does get annoying at times (and watch me have used the word “API” wrong)
When my Player object dies, the Restart scene loads with a “Replay” button. If user clicks that replay, the last level played should be loaded. Should I save the player’s progress as they finish each level so on Replay click, the last level played will be loaded?
Use Application.loadedLevel, like I stated in the first post ironically
So something like Application.LoadLevel(Application.loadedLevelName). And yes, you should either save the player’s progress at the end of each level or - not my preference - create a Don’tDestroyOnLoad temporary object for taking data from one level to another.
public void Restart(){ // For restarting the game
AutoFade.LoadLevel(Application.loadedLevelName,0.25f,Color.black,true);
// Application.LoadLevel (Application.loadedLevel);
}
In the comments there is what I originally used. The “AutoFade” thing is just for a fading effect, taken and edited from the FPS tutorial. All I can say is that I’m going to assume that a) the whole touching system works, b) the Application.Quit() works for a similar test subject (showing that the script as a whole works) and c) it’s an outside issue
He said that after the player dies he loads the restart scene,
so a call to Application.LoadLevel(Application.loadedLevel) will only again load
the restart scene.
Id propose that when your player dies you save the current level id/name
so that you can reference it from the restart scene when you want to reload the last level.