cancel
Showing results for 
Search instead for 
Did you mean: 

Unity Scene Crash After Scene Load

GamerRoman22
Explorer

Hi! I am experiencing an issue where my scene crashes the instant it loads. I have a loading scene and a script in there that loads the main scene after 15 seconds. Here is the loading script in case it is causing any problems:

using UnityEngine;
using UnityEngine.SceneManagement;
 
public class LoadingScreen : MonoBehaviour
{
    [SerializeField]
    private float delayBeforeLoading = 30f;
    [SerializeField]
    private string sceneNameToLoad;
 
    private float timeElapsed;
 
    private void Update()
    {
        timeElapsed += Time.deltaTime;
 
        if (timeElapsed > delayBeforeLoading)
        {
            SceneManager.LoadScene(sceneNameToLoad);
        }
    }
}

This script has worked before, and I am unsure why it now crashes the main scene.

Thanks!

2 REPLIES 2

movcat
Explorer

You should turn off the execution of Update after LoadScene. While Scene is loading, update may still execute even if you use Single and load immediately. Alternatively, using LoadSceneAsync is a better option to load the scene

Thanks for answering! I am not sure how scripting works yet (this is someone else's script), although I really want to learn. I've asked ChatGPT, do you mean like this?


using UnityEngine;
using UnityEngine.SceneManagement;
 
public class LoadingScreen : MonoBehaviour
{
    [SerializeField]
    private float delayBeforeLoading = 30f;
    [SerializeField]
    private string sceneNameToLoad;
 
    private float timeElapsed;
    private bool isLoading = false; // Flag to stop `Update` execution after scene load starts.
 
    private void Update()
    {
        // Stop executing `Update` after loading has started.
        if (isLoading)
            return;
 
        timeElapsed += Time.deltaTime;
 
        if (timeElapsed > delayBeforeLoading)
        {
            isLoading = true; // Mark as loading to prevent further calls.
            LoadScene();
        }
    }
 
    private void LoadScene()
    {
        // Use asynchronous scene loading.
        SceneManager.LoadSceneAsync(sceneNameToLoad);
    }
}



Thanks!