C# – Loading next level Unity c#

c++unity3d

Heres my first attempt at loading the next level upon reaching "Goal"

public int CurrentLevel = 0;
public static int maxlevel = 3;

public void CompleteLevel()
{
    if (CurrentLevel < maxlevel)
    {
        CurrentLevel += 1;
        SceneManager.LoadScene(CurrentLevel);
    }
    else
        print("you win");
}

This is in another class…

void OnTriggerEnter(Collider other)
{
    if (other.transform.tag == "DeathObject")
        Die();
    if (other.transform.tag == "Goal")
    Manager.CompleteLevel();
}

It ended up loading the first level, went to the second, then it would repeat the second map when i reached goal. Another thing it would do is skip to the last level from the first or second level.. not sure whats going on there. Heres the second thing i tried:

public void LevelPassTest()
{
    if (Application.loadedLevel > 4)
        Application.LoadLevel(Application.loadedLevel + 1);
        else
    print("win");
}

This is in another class….

void OnTriggerEnter(Collider other)
{
    if (other.transform.tag == "DeathObject")
        Die();
    if (other.transform.tag == "Goal")
    Manager.LevelPassTest();
}

I'm not sure why, but sometimes it doesn't load the next level, it worked for a while then stopped, after I reset unity it works again, so I'm not sure if it's my code or unity… I get a warning message that says "Warning CS0618 'Application.loadedLevel' is obsolete: 'Use SceneManager to determine what scenes have been loaded'" and "Warning CS0618 'Application.LoadLevel(int)' is obsolete: 'Use SceneManager.LoadScene'" I'm new to coding and completely lost on this. spent 4 hours trying to fix it. What are these warnings? can they be effecting the loading? I was also wanting to be able to save the value of the level so i can use it in game saves, and attach high scores to it. I tried using this in my first attempt, but it didn't work how i was hoping. (CurrentLevel)

Best Solution

Application.LoadLevel and the rest are obsolete as of Unity 5. You have to use SceneManager instead.

So Application.LoadLevel() is now SceneManager.LoadScene().

You must also include using UnityEngine.SceneManagement;

See here: http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

All it takes is a quick Google.

Related Question