Restart Level Help

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.

Any help would be greatly appreciated!

Application.loadedLevel or Application.loadedLevelName. So, you can even use it like:
if(GUI.button)
Application.LoadLevel(Application.loadedLevel);

Just in case I somehow messed up and for future reference:

In my Restart script, Application.LoadLevel (Application.loadedLevel) doesn’t work.

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.

Not doing anything.

Here’s a copy of my Restart script:

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();
}
}
}

I’ll try the Debug.Log as you have suggested.

Try to add your scenes to the Build Settings as well. Good luck!

Already did that, too! :smile:

Erm… Now I’m confused due to the “:smile:” ironically… Is that a honest face that says it worked, or is a sadistic face that expresses your hatred at the issue on hand?

:wink: 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. :smile:

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)

Still haven’t gotten this to work.

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 :wink:
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.

Application.LoadLevel(Application.loadedLevel) doesn’t work. I’ll try the loadedLevelName. I’ll post back in a minute.

Application.loadedLevelName doesn’t work either.

Here is my code from the Restart script

public void Update()
{
int count = Input.touchCount;
for (int i = 0; i < count; i++)
{
Touch touch = Input.GetTouch(i);
if (restartTexture.HitTest(touch.position) touch.phase == TouchPhase.Began)
{
Application.LoadLevel (Application.loadedLevelName);
}
else if (quitTexture.HitTest(touch.position) touch.phase == TouchPhase.Began)
{
Application.Quit ();
}
}
}

Here’s my bit of code:

            if(Input.touchCount > 0){
                for(int i = 0; i < Input.touchCount; i++){

                    Touch touch = Input.GetTouch(i);
                    if(touch.phase == TouchPhase.Began 
                           guiTexture.HitTest(touch.position)){

                        if (thisTexture == TextureType.Pause){
                            player.GetComponent<PlayerBall>().PauseToggle();
                        }
                        else if(thisTexture == TextureType.Retry){
                            player.GetComponent<PlayerBall>().Restart();
                        }

And here’s the code for the restarting:

    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.