My current project is set up so that when a level is finished, a GUI screen pops up with a few buttons, one of which is to advance to the next level. However, I have not coded for this since Application.LoadLevel was used, and I cannot figure out how to get the button to advance to the next level when clicked.
Here is my code:
void OnGUI()
{
if (guiShow == true) {
GUI.Box (new Rect (10, 10, 100, 90), "Victory!");
if (GUI.Button (new Rect (20, 40, 80, 20), "Next Level")) {
/*Not sure what would go here*/
}
if (GUI.Button (new Rect (20, 60, 80, 20), "Retry")) {
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
}
if(GUI.Button(new Rect(20,80,80,20), "Main Menu"))
{
SceneManager.LoadScene("Title Screen");
}
}
}
Would someone be able to help me figure this out? I have taken a look at SceneManager, and cannot figure out what I would use.
It depends on if your scenes are loaded into the index in the order of progression you’re looking for. If so, you can just grab the index of the current scene, add one (if current index is less than the scene count) and load that level. Something like this:
int cMax = SceneManager.sceneCount - 1; //Since we never want the indexer to reach the count of items
int cnt = SceneManager.GetActiveScene().buildIndex;
if(cnt < cMax)
{
cnt++;
}
SceneManager.LoadScene(cnt);
Thanks! Now, is this all supposed to go into the if statement? Because when I placed it in there, and clicked “Next Level”, it just repeated the level. If so, then I do not think I have the index set up correctly.