Unity3d – Loading Next Level


I am making a game in Unity. There are currently two levels. I count to 30 seconds, when the time becomes 0, I want to load the next level automatically. But when the next level is loaded, the game screen freezes and I cannot move anything. The function I used to load the next level follows below (this function is in a script which an empty game object which will not be destroyed when loading a new level carries):

function loadNextlvl(){
        var cur_level = Application.loadedLevel;
        yield WaitForSeconds(5.0);
        Application.LoadLevel(cur_level + 1);

What should I do?

Best Solution

My work with Unity has been hobby-driven only, but anytime I've used Application.LoadLevel I passed it a string of the level name, rather than a pointer. I see from the API that it's overloaded to take an int as well, but maybe for testing purposes, call it by name to see if that works.

Also, you need to tell Unity the levels you're going to be using. You can do this in the Build Settings off the file menu.

Lastly, you can try using Application.levelCount to see if you're within the bounds of levels.

Related Question