Application.Loadlevel obsolete

Using unity 5.4.1 and i says that Application.Loadlevel() is obsolete. So far it´s been ok to write SceneManager.Loadscene() instead but i run into trouble in this lesson.

It doesnt work when i write:
SceneManager.LoadScene(SceneManager.sceneLoaded + 1);
Instead of:
Application.LoadLevel(Application.loadedLevel + 1);

It says: The event ‘UnityEngine.SceneManagement.SceneManager.sceneLoaded’ can only appear on the left hand side of += or -=

1 Like

Hi @LuckieLuuke ,

SceneManager.sceneLoaded is used for specifying a delegate to be called once the scene has loaded.

SceneManager.LoadScene() is an overloaded method, e.g. you can use more than one variety of the same method.

The version you have used so takes a String as the name of the scene to load. There is a second which takes in an int which represents the build index for the scene from your build settings.

As per the documentation however, it is recommended that you use SceneManager.LoadSceneAsync() instead. This also provides an overloaded method where you can specify the build index of the scene to load instead of the name.

Just as a note of caution; in the Unity course, from memory you have the scenes in this order;

  • 01 - Start Main
  • 02 - Level_01
  • 03 - Level_02
  • 04 - Level_03
  • 05 - Loose Screen
  • 06 - Win Screen

You will want to check that the conditions for starting a new game, losing, and winning all use the method which takes in a String and specify the name of the scene - for example, when a player completes Level_03, if you don’t specify that this is the last playable scene and call Win, you will end up calling LoadSceneAsync() and pass in the current scene build index + 1 - which will take you to the Loose Screen.

You may also want to consider re-ordering the scenes.


In order to obtain the active scene build index you could use something like;

private int _activeSceneBuildIndex = 0;

and

Scene activeScene = SceneManager.GetActiveScene();

_activeSceneBuildIndex = activeScene.buildIndex;

SceneManager.LoadSceneAsync(++_activeSceneBuildIndex);

You would probably want to store this in your levelManager and set it from a method call in your Start() method, perhaps;

void Start()
{
    Initialise();
}

private void Initialise()
{
Scene activeScene = SceneManager.GetActiveScene();

_activeSceneBuildIndex = activeScene.buildIndex;
}

Then update the methods where you are actually loading the next scene with;

SceneManager.LoadSceneAsync(++_activeSceneBuildIndex);

Hope this helps.

4 Likes

Thanks, thats very interesting and solves my problem.

1 Like

You’re more than welcome :slight_smile:

Could you please post the whole levelManager code with your changes built in?

I tried to implement the changes you suggested above but I can’t get it to work (my experience is limited). Thank you.

Hi @madsix,

Just to check, were you asking me or @LuckieLuuke? I only ask as what I had at the time of writing would be considerably different the basic project as a whole, so what I did for Luke was to extract parts from it and then piece together here. I would be happy to do the same for yourself if it is helpful. Let me know, and if so, pop a copy of your current levelManager code up for me too please.

Hi @Rob,

thank you very much for your help. Any help is appreciated :slight_smile:
This is my code now, I have been messing around a bit:

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

public class LevelManager : MonoBehaviour {

	void Start () {
		Initialize ();
    }

	private void Initialize() {
		Scene activeScene = SceneManager.GetActiveScene();
		_activeSceneBuildIndex = activeScene.buildIndex;
	}

	public void LoadLevel (string name){
		Debug.Log("Level load requested for: " + name);
		//Application.Loadlevel (name);
		SceneManager.LoadSceneAsync(name);
	}

	public void QuitRequest(){
		Debug.Log("I want to quit!");
		Application.Quit();
	}

	private int _activeSceneBuildIndex = 0;

	public void LoadNextLevel() {		
		SceneManager.LoadSceneAsync(++_activeSceneBuildIndex);	
	}
 }

Messing around is good! That’s how you’ll start to learn what things work, and what doesn’t, and when it doesn’t how you fix it! Well done :slight_smile:

So… was there a specific issue at the moment, are your scenes not loading, or is it a case of trying to get a Unity5+ version of the LevelManager working?

@Rob I am trying to get the Levelmanager to work with the recent Unity version. Nothing happens when the ball hits the bricks. As I was typing this reply I got the idea that maybe my Brick script was wrong. But it seems right, too. Hope you can help.

using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour {

	public int maxHits;

	private int timesHit;
	private LevelManager levelManager;

	// Use this for initialization
	void Start () {
		timesHit = 0;
		levelManager = GameObject.FindObjectOfType<LevelManager>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnCollissionEnter2D (Collision2D col)	{
		timesHit++;
		Destroy(gameObject);
	}

// TODO Remove this method once we can actually win
	void SimulateWin () {
		levelManager.LoadNextLevel();
	}

}

HI @madsix, ok, so lets gain an understanding (or at least help me gain one) of what is actually happening, step by step.

So, at this time, can you actually run the game (e.g. click Run within Unity) and it starts without error?

If so, do you have a menu scene where you can click Start?

If so, can you click on start and be taken to the first scene with blocks in it?

Is there a ball on the paddle in the middle of the screen?
Does the ball launch when you click the mouse?

If the ball does launch, what happens when it hits a block? Does it stop / bounce off / bounce off and destroy the block?

If you could answer the above for me that would be great as it will give me a bit of an idea as to where you are.

Hi @Rob, sure:

1 Like

Thanks for that, that’s really helpful. I’m actually looking at the LevelManager for you now. I’ll put something together and then perhaps you can give it a test, I’m actually in between a few things, so bear with me :slight_smile:

@Rob take all the time you need. Thank you very much, I really appreciate it!

1 Like

Shouldn’t take me long, but you’ll need to do the testing and probably change a couple of methods calls in other scripts as a couple will be renamed.

If the block still doesn’t get destroyed after we have this in place and error free then we’ll start some diagnostics on that pesky block! :slight_smile:


Updated Wed Nov 23 2016 19:51

Ok @madsix, give this a try, I took the finished project from the Block Breaker section of the course and have amended the code to allow for the changes to Unity5 (SceneManagement instead of Application.LoadLevel etc).

Be aware that I have renamed a few things to reflect these changes, this wasn’t particularly necessary, but I like things tidy :slight_smile: So… LevelManager is now called SceneController, you will need to rename the file accordingly, e.g. SceneController.cs (classes and filenames must match in Unity). You will most likely have calls to methods in other scripts that will be unhappy with this renaming, so those will need to be updated also to reflect the changes.

After you have done this, be sure to check that it’s actually still attached to the relevant objects in the scene(s).

I have left the Debug.Log() lines in, but commented them out, so once this is in place and hopefully error free, if your block doesn’t get destroyed and the next level doesn’t load, you can uncomment these and check to see if its calling the load of the next scene.


SceneController.cs

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneController : MonoBehaviour
{
    //  NOTE:   Be aware that loading scenes by SceneBuildIndex will require you 
    //          to have your scenes in a specific order, see this post for details;
    //          https://community.gamedev.tv/t/solved-application-loadlevel-obsolete/8200/2

    private int _activeSceneBuildIndex = 0;

    /// <summary>
    /// Initialisation
    /// </summary>
    void Start()
    {
        Initialise();
    }

    /// <summary>
    /// Initialises the SceneController
    /// </summary>
    private void Initialise()
    {
        Scene activeScene = SceneManager.GetActiveScene();

        _activeSceneBuildIndex = activeScene.buildIndex;
    }

    /// <summary>
    /// Loads a scene for the specified name
    /// </summary>
    /// <param name="name">The name of the scene to load</param>
    public void LoadSceneAsync(string sceneName)
    {
        // Debug.Log("New Scene load: " + sceneName);
        Brick.breakableCount = 0;
        SceneManager.LoadSceneAsync(sceneName);
    }

    /// <summary>
    /// Loads the next scene in order of build index
    /// </summary>
    public void LoadNextSceneAsync()
    {
        Brick.breakableCount = 0;
        SceneManager.LoadSceneAsync(++_activeSceneBuildIndex);
    }

    /// <summary>
    /// Checks to see if the final brick has been destroyed and loads the next scene if so
    /// </summary>
    public void BrickDestoyed()
    {
        if (Brick.breakableCount <= 0)
        {
            LoadNextSceneAsync();
        }
    }

    /// <summary>
    /// Handles a request to quit the game
    /// </summary>
    public void QuitRequest()
    {
        // Debug.Log("Quit requested");
        Application.Quit();
    }
}

Let me know how it goes :slight_smile:

10 posts were split to a new topic: [Help] Application.LoadLevel / Restarting my game

@Rob thanks a lot, now it works! I have to make some adjustments to correctly switch to the correct scene, but thanks to your explanation this is easy. I can actually play now, that’s awesome. Thanks again :blush:

1 Like

Really pleased to hear you have it all working now - well done :slight_smile:

Actually, you can do this without two methods:

 	public void LoadNextLevel() {
 		Scene activeScene = SceneManager.GetActiveScene();
 		int NextSceneIndex = activeScene.buildIndex + 1;
 		SceneManager.LoadScene(NextSceneIndex);
 	}
2 Likes

You can also do it in fewer lines of code

public void LoadNextLevel()
{
    Scene currentScene = SceneManager.GetActiveScene();
    SceneManager.LoadScene(currentScene.buildIndex + 1);
}

or even fewer :wink:

public void LoadNextLevel() {
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
1 Like

Privacy & Terms