- Home /
SceneManager.LoadScene causes total freeze
So my game has a main menu scene (scene 0) and an ingame scene (scene 1). When i try to load scene 0 from 1 it freezes forever and a memory leak occurs. I've tried async and that didnt work either and ive tried to make sure every value and etc is valid.
I thank you in advance for any help.
One thing to note about the script is that the Debug.Log("MainMenu input");
doesnt even show in Console. The script:
public class LevelController : MonoBehaviour
{
[SerializeField]
private Transform[] playerSpawns;
[SerializeField]
private GameObject playerPrefab;
[SerializeField]
private GameObject winScreen;
[SerializeField]
private TextMeshProUGUI winText;
private PlayerConfiguration[] playerConfigs;
private List<int> deadPlayers = new List<int>();
public bool gameOver;
void Start()
{
playerConfigs = PlayerConfigurationManager.Instance.GetPlayerConfigs().ToArray();
for (int i = 0; i < playerConfigs.Length; i++)
{
playerConfigs[i].PlayerObj = Instantiate(playerPrefab, playerSpawns[i].position, playerSpawns[i].rotation, gameObject.transform);
playerConfigs[i].SetAlive(true);
playerConfigs[i].Input.enabled = true;
playerConfigs[i].PlayerObj.GetComponent<Renderer>().material = playerConfigs[i].playerMaterial;
playerConfigs[i].PlayerObj.GetComponent<PlayerInputHandler>().InitPlayer(playerConfigs[i]);
}
gameOver = false;
Debug.Log("Number of players: " + playerConfigs.Length);
}
void Update()
{
if (!gameOver)
{
for (int i = 0; i < playerConfigs.Length; i++)
{
// Handles DeathCheck and stuff.
if (playerConfigs[i].isAlive())
{
if (playerConfigs[i].PlayerObj.transform.position.y < -10f)
{
playerConfigs[i].isAlive();
deadPlayers.Add(i);
playerConfigs[i].Input.enabled = false;
Destroy(playerConfigs[i].PlayerObj.GetComponent<Mover>().arrow);
Destroy(playerConfigs[i].PlayerObj);
}
}
if (deadPlayers.Count == (playerConfigs.Length - 1))
{
for (int e = 0; e < playerConfigs.Length; e++)
{
if (!deadPlayers.Contains(e))
{
Debug.Log(playerConfigs[i].PlayerIndex.ToString() + " has won!");
gameOver = true;
SetWinner(e);
}
}
}
}
}
}
private void SetWinner(int i)
{
var img = winScreen.GetComponentInChildren<UnityEngine.UI.Image>();
img.color = playerConfigs[i].playerMaterial.color;
winText.SetText("Player " + (i + 1).ToString() + " has won!");
winScreen.SetActive(true);
// ###########################################
// FIX THIS LATER ############################
//############################################
// derps out when restarting level
playerConfigs[i].Input.onActionTriggered += Input_onActionTriggered;
Debug.Log("Player " + i + " input actions added");
}
private void Input_onActionTriggered(CallbackContext obj)
{
if (obj.action.name == "MainMenu")
{
Debug.Log("MainMenu input");
if (GameObject.Find("LevelInitializer").GetComponent<LevelController>().gameOver)
{
GoToMainMenu();
}
}
else if (obj.action.name == "Restart")
{
Debug.Log("Restart input");
if (GameObject.Find("LevelInitializer").GetComponent<LevelController>().gameOver)
{
PlayAgain();
}
}
}
private void PlayAgain()
{
for (int i = 0; i < playerConfigs.Length; i++)
{
}
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name, LoadSceneMode.Single);
}
private void GoToMainMenu()
{
Debug.Log("GoToMainMenu()");
while (true) {
GameObject g = GameObject.Find("PlayerConfigurationManager");
if (g == null)
{
break;
}
Destroy(g);
}
Debug.Log("loading Main Menu");
SceneManager.LoadScene(0, LoadSceneMode.Single);
}
}
Answer by rh_galaxy · Sep 15, 2020 at 04:24 PM
I can't explain why "Main menu input" isn't showing, maybe because it's called from a callback. Most likely your GoToMainMenu() will loop forever, because Destroy() will not destroy the gameObject directly only mark it to be destroyed later, and Find() will find it again so your while-loop will never be done.
Also do you set your LevelController as a "Do not destroy" object so it lives during the whole project? Look up singleton and how to do it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613011911im_/https://answers.unity.com/themes/thub/images/avi.jpg)