- Home /
SceneManager.SceneLoad problem.
Since Unity was updated to its last version , the use of Application.LoadScene was obsolete , i started using SceneManager.LoadScene instead , and i had a big problem im unable to solve.
When i m in my main menu scene and i choose a level to play , it all loads perfectly , i load the splash screen and then load the scene number according to my choice in the main menu, but when i try to go back from my level to my main menu to choose another level , it loads back to my splash screen , loads my second level for a second , then go back to my main menu scene , and its driving me crazy.
i tried searching on the community for any bugs to that matter , but no luck.
anyone can relate or can give me a solution to this problem?
You've clearly got a mistake, but without seeing your code, it's pretty impossible to point out where...
Answer by aboabed · May 08, 2016 at 09:19 PM
my game consists of 3 levels ( Scenes) , a splash Scene and a main menu Scene.
The main menu Scene is consisted of a GameObject Scene that is passed between different scenes, it have a script that hold the value of the next Scene , the script is called (SceneNumber) , when i choose a scene the character walks towards its specific collider that represent the scene.
MainMenuSceneSelect hold the collisions of that player with the specified scene collider.
MainMenuSceneSelect.cs
public GameObject SurvivalScene;
public GameObject EndlessRunScene;
public GameObject TrollScene;
GameObject scene;
void Start()
{
scene = GameObject.FindGameObjectWithTag ("Scene");
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == SurvivalScene) {
SceneManager.LoadScene (4);// Loads the splash scene
scene.GetComponent<ScenesNumber>().sceneNumber = 1; //each represents the scene number in build
} else if (other.gameObject == EndlessRunScene) {
SceneManager.LoadScene (4); // Loads the splash scene
scene.GetComponent<ScenesNumber>().sceneNumber = 2; //each represents the scene number in build
} else if (other.gameObject == TrollScene) {
SceneManager.LoadScene (4);// Loads the splash scene
scene.GetComponent<ScenesNumber>().sceneNumber = 3; //each represents the scene number in build
}
}
after that the splash scene is loaded , i put an object to collide with a specific gameobject to end the splash scene and go to the specified scene number in the GameObject Scene , that holds the next scene number.
//note : i already made the Scene gameobject not to be destroyed on load .. // DontDestroyOnLoad()
EndSplash.Cs :
public GameObject troll;
GameObject scene;
void Start () {
scene = GameObject.FindGameObjectWithTag ("Scene");
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == troll) {
if (scene.GetComponent<ScenesNumber> ().sceneNumber == 0) {
Destroy (scene); //destroy the Scene Gameobject in case of returning to the mainmenu scene so we dont have doubles of Scene GameObject.
SceneManager.LoadScene (scene.GetComponent<ScenesNumber> ().sceneNumber);
} else {
SceneManager.LoadScene (scene.GetComponent<ScenesNumber> ().sceneNumber);
}
}
}
now , the first time we do that it all works fine , the intended scene gets loaded and there is no problems at all , but when i use the pause menu to return to the MainMenu Scene by
PauseManager.cs
GameObject scene;
void Start () {
Time.timeScale = 1;
scene = GameObject.FindGameObjectWithTag ("Scene");
}
// Update is called once per frame
void Update () {
if (CrossPlatformInputManager.GetButton ("Pause")) {
Time.timeScale = 0;
} else if (CrossPlatformInputManager.GetButton ("Resume")) {
Time.timeScale = 1;
}
else if (CrossPlatformInputManager.GetButton ("MainMenu")) { // for the menu return
Time.timeScale = 1;
SceneManager.LoadScene (4); // the splash scene
scene.GetComponent<ScenesNumber>().sceneNumber = 0;
}
}
and it loads the MainMenu Scene without any problems , the double is destroyed and i have a GameObject Scene in the MainMenu Scene and its all good.
the problem is when i try to do the exact same thing all over again , the second i load the splash screen into any scene , the splash screen is reloaded and it goes directly to the main menu scene all over again without even starting the intended level.
i hope i've been specific enough for you to understand my problem.