Scenes not loading in the correct order
Hi, I'm currently creating a basic unity 3D game and I have come across a problem with the scenes not loading in the correct order.
I have a main menu , 3 levels, a 'You win' and finally a 'You lose' scene (making a total of 6 scenes in that build order.)
However after clicking new game, it enters level one, upon completing level 1 it repeats level 1, then upon completing level 1 again it will successfully load level 2, and then upon completing level 2 it jumps straight to the 'You win' scene. I am new to unity and coding in general and feel i am missing something relatively obvious but i have been looking for hours for a solution and found nothing. Any help would be appreciated!
Here is the GameManager part involved:
using UnityEngine; using UnityEngine.SceneManagement; using System.Collections; public class GameManager2 : MonoBehaviour{
public int currentScore;
public int highscore;
public static int currentLevel = 0;
void Update()
{
startTime -= Time.deltaTime;
currentTime = string.Format("{0:0.0}", startTime);
if (startTime <= 0)
{
startTime = 0;
SceneManager.LoadScene(5);
}
}
public void CompleteLevel()
{
if (currentLevel < 4)
{
//this loads levels in sequence
SceneManager.LoadScene(currentLevel += 1);
}
else {
//this is the win settings
SceneManager.LoadScene(4);
print("You win");
}
}
Here is the MainMenu part involved:
public void LoadScene(string name)
{
SceneManager.LoadScene(name);
}
public void QuitGame() {
Application.Quit();
}
}
I was worried i had put them in the wrong order in the build settings so I removed all levels and placed them all back in in the correct order but that didn't help. As far as i can tell they are definitely in the correct order (but as i said i am new to unity).
thanks for reading and any help would be greatly appreciated :)
you've initialized your level number like this:
public static int currentLevel = 0;
so, on the first load in CompletedLevel()
, it'll become 1... not sure why level 3 wouldn't load. is that the only place you're increasing the counter?
try using named level names ins$$anonymous$$d... then the order won't be important either ;)
Hey, thanks for the reply. in my player controller script i have this for completing the level.
void OnTriggerEnter (Collider other)
{
if (other.transform.tag == "Goal")
{
manager.CompleteLevel();
}
}
Could this issue be that i am only completing the level upon reaching the goal? do i need to set up a way to complete the level at the mainmenu to?
Answer by Shire_ · Apr 20, 2016 at 03:06 PM
FIXED!
finally found a fix in the end!
needed to use:
if (currentLevel < 4) { currentLevel = SceneManager.GetActiveScene().buildIndex+1; SceneManager.LoadScene(currentLevel); }
Your answer
Follow this Question
Related Questions
Audio and Scene Load Troubles 0 Answers
Game is Glitched After Reloading the Next Day 0 Answers
After reloading the scene twice one script doesn't work. 0 Answers
obbdownloader relevance 0 Answers