Eventually managed to get scene switch working
Game freezes after button is clicked to switch scenes
Hello,
I recently started 2D mobile development on Unity and don't know a whole lot about it as of yet (learning as I go). For the most part everything has been going fine, but there is one error I have come across that I can not find out at all.
When I run the game, its starts up fine and everything loads properly, but as soon as I swap scenes a few times using the buttons handling onClick(), it causes the app to freeze.
These are the buttons I use to switch scenes, which are located on each scene:
I have a small script setup for switching at the moment (I know it can be simplified to a single function and little amount of variables, this is just to help myself with referencing)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneControl : MonoBehaviour {
public void dungeonClick () {
SceneManager.LoadScene ("DungeonScreen", LoadSceneMode.Additive);
SceneManager.UnloadSceneAsync ("TavernScreen");
SceneManager.UnloadSceneAsync ("ActivitiesScreen");
}
public void tavernClick () {
SceneManager.LoadScene ("TavernScreen", LoadSceneMode.Additive);
SceneManager.UnloadSceneAsync ("DungeonScreen");
SceneManager.UnloadSceneAsync ("ActivitiesScreen");
}
public void activitiesClick () {
SceneManager.LoadScene ("ActivitiesScreen", LoadSceneMode.Additive);
SceneManager.UnloadSceneAsync ("DungeonScreen");
SceneManager.UnloadSceneAsync ("TavernScreen");
}
}
The app originally froze when I first clicked to switch scenes, and I don't know what I did to lengthen the amount of time before it freezes. It went from first switch of a scene to four. I have tried looking through google for the longest time for a solution but all I can find is about LoadScene destroying the previous scene and creating the new scene.
I am separately unloading the other scenes because when I left the LoadScene normally, it would hang instantly on the button press after switching the scenes. When I tried this way it would allow switching a few times before it froze on a scene again.
I do have the C# Script in each scene connected to the canvases. I don't know what else may be helpful to understanding the problem I am having, but the error may be something very simple that I haven't realized for the past hours or so that maybe someone knows.
I'd like to know what happened when you used LoadScene (not additive) only? or is that what you did. if that already causes a freeze of try to replicate it in the editor. if that doesn't work check the profiler while running an development build.
I have done the non-additive prior to using additive and that would cause it to freeze instantly after button press. I have found an error where it cant find a scene to unload, so I'm guessing that's what's causing the game to hang. Trying to find a way around it but no luck. Is there any way to open a scene without destroying the other scene at all, and not having multiple of the same scene open? Because there would be a lot of going back and forth between the scenes and it really starts to slow it down after 10 switches when all scenes are set to additive and none get destroyed.
I just read the docs about UnloadSceneAsync and it essentially destroys all scene content like a normal LoadScene would do, just async.
If it's not too much of a load, why not loading all three scenes once and just change the current active one?
Answer by WildRyce · Mar 09, 2017 at 08:49 PM
Managed to figure out a way to load and unload certain scenes using a master scene without it causing any errors.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ButtonControl : MonoBehaviour {
public void dungeonClick () {
SceneManager.UnloadSceneAsync ("TavernScreen");
SceneManager.LoadScene ("DungeonScreen", LoadSceneMode.Additive);
}
public void tavernClick () {
SceneManager.UnloadSceneAsync ("DungeonScreen");
SceneManager.LoadScene ("TavernScreen", LoadSceneMode.Additive);
}
}
Thank you @hexagonius for your help overall, got me thinking of a better way to load the scenes. Truly appreciated :D and surprised I didn't think of doing this earlier.