Application.Loadlevel obsolete

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

If you didn’t say it, I was just about to. You were the very last post. :grinning:

Hi Rob,

I’m having a little problem, and I’m not sure how to solve it. Love your videos by the way, I followed the tutorial for block breaker, and I reached the point where I load next level when all blocks are destroyed. I played the game from stage 1 to 3 , and I beat the game everything works fine except when I lose on any stage, and I restart the game, after I destroy all the blocks the next level won’t load, even though it worked during the first play through, so what do you think is the problem? thank you

Regards
Ahmad

Privacy & Terms