Application.LoadLevel / Restarting my game

Hi guys!
Unity 5 said that to me, Application.Loadlevel() is obsolete, but it’s working anyway. I have one problem, when I start the game, finish the level then game load next one, but if I for example finish first and second, but fail in 3rd one and start again, game will not loadcany next level :frowning: any ideas what is wrong?

This is my code from LevelManager and sorry for my English :stuck_out_tongue:

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

	public void LoadLevel (string name)
	{
		Application.LoadLevel(name);
	}
	public void QuitRequest ()
	{
		Application.Quit();
	}

	public void ZniszczoneLoad ()
	{
		if (Brick.zniszczalnyCount <= 0) {
		LoadNextLevel();
		}
	}
	public void LoadNextLevel ()
	{
		Application.LoadLevel (Application.loadedLevel + 1);
	}
}

What do you have set up for the button that you click on to restart the game? If you select the button on the lose scene, then take a screen shot of the Inspector with the Button Script viewable on the right…

unfortunetly i have to work all weekend 12 h per day, so I’ll post you everythung at Tuesday :frowning:

Sure @Marcin_Radczyk, when ever you’re ready, I will be here :slight_smile:

here you go Rob

I added Debug.Log(“Next level requested”) in my levelManager script, and I found out, that game stops using level manager when I use “RESTART” on lose scene, only loadnext level is working on the Start scene :confused: maybe i should use SceneManager functions instead of loadNextLevel?

It will come down to what methods you are calling and from where.

From memory, isnt there a Button component added to the Start text, which has an onClick evemt setup to call a specific method.

Equally your Restart text will have an event wired to it also. So, paste up your code, lets have some screenshots of the potential problem areas, e.g. select the Start text ans then get a screenshot of the inspector so we can see which events are being called and the same for the Restart text when you lose.

Also, confirm for me that I understand what you are trying to do… I’ll write it out like a mini story.

  • I want to press Start and be taken to the first level
  • If I lose on this level I want to be able to click Restart and be taken back to the first level to play again, not the Menu.
  • If I click on Menu I want to be returned to the Menu
  • If I complete the first or subsequent levels I want to be taken to the next level
  • If I lose on any subsequent level the Restart option will allow me to play that level again, without returning to the Menu
  • If I click on Menu I will be taken back to the Menu
  • If I complete all levels I want to see Win scene with an option to return to the Menu

This is what I am assuming so please do correct me on any of these steps that are not correct, with this and the info above we will work through it together :slight_smile:

_Updated Tue Nov 29 2016 17:43_

In your screenshot above there is an example of what I mean. Your onClick event for the Restart text is xalling the Start() method within LevelManager. So, in order to have Restart do something differnet it will need a different method to call, one which can load the last level that was played. So, you will most likely want a variable to store that information too. (sorry, I misread your screenshot!)

1 I want to press Start and be taken to the first level
2 If I lose on this level I want to be able to click Restart and be taken back to the first level to play again, not the Menu. Everytime I press RESTART buttom I will start at first level and I’ll have to beat all game again ( for now :slight_smile: )
3 If I click on Menu I want to be returned to the Menu
4 If I complete the first or subsequent levels I want to be taken to the next level
5 If I click on Menu I will be taken back to the Menu
6 If I complete all levels I want to see Win scene with an option to return to the Menu

This is my LevelManager code

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class LevelManager : MonoBehaviour { 	

    // will load level by name
    public void LoadLevel (string name)
    {
    	Application.LoadLevel(name);
    	Debug.Log(Application.loadedLevel);
    }

    // will close application when qouit button is clicked
    public void QuitRequest ()
    {
    	Application.Quit();
    }

	// will load next level if all bricks are destroyed
    public void ZniszczoneLoad ()
    {		
        if (Brick.zniszczalnyCount <= 0) {
    	    LoadNextLevel();
    	}
    }

    public void LoadNextLevel ()
    {
    	Debug.Log("Next level requested " + (Application.loadedLevel + 1));
    	Application.LoadLevel (Application.loadedLevel + 1);
    }
}

and here are screen shots from Start, Win and Lose screen

  1. Start Screen
  1. Win Screen
  1. Lose Screen - Restar Button

1 I want to press Start and be taken to the first level

Start scene - At the moment the OnClick() event in the Button component on your Przycisk Start Text is set to call the LoadNextLevel() method within LevelManager. Whilst the code you have is using the deprecated methods (e.g. Application.loadedLevel) this should still work.

Please confirm step 1 is working as expected.

2 If I lose on this level I want to be able to click Restart and be taken back to the first level to play again, not the Menu. Everytime I press RESTART buttom I will start at first level and I’ll have to beat all game again ( for now :slight_smile: )

Lose scene - At the moment the OnClick() event in the Button component on your Restart Text is set to call the LoadLevel() method within LevelManager, passing it the scene name Level_01. Whilst the code you have is using the deprecated methods (e.g. Application.loadedLevel) this should still work.

Debug.Log(Application.loadedLevel); this line within the LoadLevel(string name) will not be called. You request that a different level is loaded before it, when that level loads a new LevelManager object is instantiated, the one that you were using is destroyed, the Debug.Log statement is lost.

Move the Debug.Log(Application.loadedLevel); statement above the Application.LoadLevel(name); statement.

The expected output in the console would be the index of the level being loaded, if you are restarting then I would expect this to be index 2. If you were returning to the Menu then index 1. (see item 4 below regarding scene index order).

3 If I click on Menu I want to be returned to the Menu

Win scene - At the moment the OnClick() event in the Button component on your MenuStart Text is set to call the LoadLevel() method within LevelManager, passing it the scene name Start. Whilst the code you have is using the deprecated methods (e.g. Application.loadedLevel) this should still work.

Please confirm that Step 3 is working as expected.

4 If I complete the first or subsequent levels I want to be taken to the next level

LevelManager script - Looking at the method ZniszczoneLoad() the code contained within looks like it would successfully call the LoadNextLevel() method, which in turn would load the next level, based on the level index number. For example, if Level_04 is index number 5, then it would try to load the scene with the index of 6, presumably this is you Win scene?

Please confirm from your build settings the indexes of the scenes. You would probably want them in this order for now (Start, Level_01, Level_02, Level_03, Level_04, Win, Lose)

Please confirm that Step 4 is working as expected, that you progress from one level to the next, and if you are successful you get all the way to the Win scene.

5 If I click on Menu I will be taken back to the Menu

See Item 3 above.

6 If I complete all levels I want to see Win scene with an option to return to the Menu

Please confirm that this works as expected.


So, please respond to each part of the above and we will see where we are from that. Tagging you so that you can find this new topic more easily @Marcin_Radczyk :slight_smile:

Sorry you have to wait for so long, but I finally solve the puzzle. After lose and restart the game, zniszczalneCount hold value from last loaded scene, so I just had to reset zniszczalneCount in loseCollider just before loading new scene :slight_smile:

But anyway thank you very much for support!!! :smiley:

1 Like

Glad to hear you resolved it, well done :slight_smile:

1 Like

Could you, please, mark this as [Solved]? :wink:

1 Like

Privacy & Terms