Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
GamerRoman22's avatar
GamerRoman22
Explorer
12 months ago

Unity Scene Crash After Scene Load

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

Replies have been turned off for this discussion
  • 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

    • GamerRoman22's avatar
      GamerRoman22
      Explorer

      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!