[Solved]At Loose screen retry the level what u're before

Hey! I’ve got stucked with an issue about the Loose Screen.

I was trying to get work a Loose Screen where if you loose the second level you wont have to start over again at first level when you click onto “Retry” the second level gets loaded.

This is what I tried out:

public int levelsCompleted = 0;
public void BrickDestroyed(){
	if (Brick.breakableCount <= 0){
		LoadNextLevel++;
		print ("levelsCompleted: "+levelsCompleted);
		LoadNextLevel();
		Brick.breakableCount = 0;
	}
}
public void LoadOnLevel(){
		if (levelsCompleted == 0){Application.LoadLevel("level_01");}
		else if (levelsCompleted == 1){Application.LoadLevel("level_02");}
		else if (levelsCompleted == 2){Application.LoadLevel("level_03");}
		else if (levelsCompleted == 3){Application.LoadLevel("level_04");}
		else if (levelsCompleted == 4){Application.LoadLevel("level_05");}
		else if (levelsCompleted == 5){Application.LoadLevel("level_06");}
		else if (levelsCompleted == 6){Application.LoadLevel("level_07");}
		else if (levelsCompleted == 7){Application.LoadLevel("level_08");}
		else if (levelsCompleted == 8){Application.LoadLevel("level_09");}
		else if (levelsCompleted == 9){Application.LoadLevel("level_10");}
		else if (levelsCompleted == 10){Application.LoadLevel("level_11");}
		else if (levelsCompleted == 11){Application.LoadLevel("level_12");}
		else {}
	}

I’ve set the retry button LoadOnLevel but keeps starting level 1 first.

Any suggestion?

Why not just store the level name of the last level completed in a class level variable, then, when you die, load that level. You wouldnt need all of the if/else logic either.

This is what I tried but it keeps sending me into the start screen:

private int lastCompletedLevel;
public void BrickDestroyed(){
		if (Brick.breakableCount <= 0){
			lastCompletedLevel = Application.loadedLevel;
			LoadNextLevel();
			Brick.breakableCount = 0;
		}
	}
public void LastLevel(){
		Application.LoadLevel(lastCompletedLevel);
	}

This presumably isn’t all of the code? I guess you have a button or something when the player dies that they click to restart from where they were?

Isn’t is just the code referring to the problem.

Here is all of the code:

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {
	private int lastCompletedLevel;
	public void LoadLevel(string name){
		Debug.Log("Level load requested for: "+name);
		Application.LoadLevel(name);
		Brick.breakableCount = 0;
	}
	public void QuitRequest (){
		Debug.Log("I Want to quit");
		Application.Quit();
	}
	public void LoadNextLevel(){
		Application.LoadLevel(Application.loadedLevel +1);
		Brick.breakableCount = 0;
	}
	public void BrickDestroyed(){
		if (Brick.breakableCount <= 0){
			lastCompletedLevel = Application.loadedLevel;
			LoadNextLevel();
			Brick.breakableCount = 0;
		}
	}
	public void LastLevel(){
		Application.LoadLevel(lastCompletedLevel);
	}
}

Sometimes, and other times people give you a snippet and it’s not until 50 posts later when a screenshot or another bit of code suddenly appears that you realise what the problem is… I tend to prefer to see as much as possible up front, just saves everyone time :slight_smile:

Before I saw your post I had a thought… when you are testing this, are you testing it on the first playable scene, or are you destroying all of the blocks on a scene and then getting to the next, and then losing?

From the above, if a player loses on the first level, if you still provide them with a restart option the variable lastCompletedLevel won’t be set to anything.

Can you pop a screenshot up of the Build Settings also, this will show the order of the scenes in the build.

I break all the blocks at first level and than when I move to the next scene and when I lost the Lose Scene pops up as usual, but when I click on to Play Again it’s just put me into the start scene the Button Setted up correctly.

There is a screenshot from build settings:

Oooh… hang on…

You have a LevelManager on each scene don’t you? So, when the next scene loads, the previous LevelManager is destroyed, and along with it your lastCompletedLevel variable.

put the word static in front of it for now, just to see if it works… e.g.

private static int lastCompletedLevel;

Also, thinking about it, don’t you want to let them restart from the level they actually lost on, rather than the previous level? so it should probably really be lastPlayedLevel, and whether they destroyed all the blocks or not is actually irrelevant isn’t it… so you’d want to move where you set it to your LoadNextLevel() method.

Alternatively, within LastLevel(), use this’

Application.LoadLevel(lastCompletedLevel+1)

I’ve tried and both recommendations helped out a lot! :slight_smile:

The first problem I make i don’t set the int variable into static.

The second is I dont +1 this:

Application.LoadLevel(lastCompletedLevel+1)

Thank you for helping me through this :slight_smile: and also thank you for your advices :slight_smile: .

1 Like

Glad you have got it working, well done, and you’re more than welcome.

As a suggestion, might be nice to have two buttons when the player loses, one that says “Restart” which goes back to the beginning of the game, and maybe another that says “Continue”, and that’s the one that runs the above… just a thought.

Take it steady :slight_smile:

Privacy & Terms