Home > Enterprise >  Unity issues loading scene for the second time
Unity issues loading scene for the second time

Time:07-13

I ran into a very strange problem. When the scene is loaded for the first time, everything works fine, it loads. But then I go to the game menu and start the game again, the loading scene starts and nothing happens. Here is the code:

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class LoadMenu : MonoBehaviour
{
    private AsyncOperation asyncOperation;

    private void Start()
    {
        StartCoroutine(LoadGame());
    }
    
    IEnumerator LoadGame() {
        yield return new WaitForSeconds(1);
        print("start loading");
        asyncOperation = SceneManager.LoadSceneAsync("Game");
        asyncOperation.allowSceneActivation = true;
    }
}

CodePudding user response:

I have created a blank project with your script and it works perfectly. In short I copied your script; I added the script to a gameObject, I created a new scene; I named the scene "Game"; I added the scene to the build settings and ran the game. The game moves from the first scene where the script is to the second in no time. Did you mention that there are no console errors or other warnings? If so, then for sure there is a problem with the rest of the game. At least the message is shown: start loading? If it is not even shown I suspect that the script is not linked to a disabled GameObject or gameObject. To find out, insert a print () in the Start () method. If it is called, the problem lies with the rest of the project.

CodePudding user response:

The answer was very simple. In the pause menu, I set Time.timeScale = 0; And then I forgot to set it to 1. Because of this, yield return new WaitForSeconds() simply did not work as it should. xd

  • Related