简体   繁体   中英

Coroutine wont load new scene after wait for 3 seconds

I got this script. The idea is to trigger LoadNextScene() after finishing the game. This will load a scene called "Well done". This scene shall be open for about three seconds and after that load the scene called "Start menu".

This works half the way. When the game is finished "Well done" scene is loaded but the "Start menu" isnt loaded and the debug message in MyCoroutine() isnt printed.

What can be wrong?

Im also thinking about if I shall stop my coroutine as good practise the way I do in the code?

Here is my code:

public void LoadNextScene()
{   
    Debug.Log("NEXT SCENE WAIT 3 sec");
    SceneManager.LoadScene("Well done");
    Debug.Log("Start waiting");
    StartCoroutine(MyCoroutine());
    StopCoroutine(MyCoroutine());
}

private IEnumerator MyCoroutine()
{
    yield return new WaitForSeconds(3);
    Debug.Log("Finish waiting and load start menu");
    SceneManager.LoadScene("Start Menu");
}

Well after your very first SceneManager.Load the current Scene this script instance belongs to is unloaded → this GameObject destroyed → Nobody is running your Coroutine anymore.

For some reason you additional used StopCoroutine right after starting it so it wouldn't run anyway → No this makes no sense;)


Alternatively you could simply have a separate dedicated script in your Well Done scene which automatically goes back to the main menu after 3 seconds.

Actually to make it more flexible and reusable you could have a component like

public class SwitchSceneDelayed : MonoBehaviour
{
    // Configure these in the Inspector
    [SerializeField] bool autoSwitch = true;
    [SerializeField] float _delay = 3f;
    [SerializeField] string targetScene = "Start menu";

    private void Start()
    {
        if(autoSwitch) StartCoroutine(SwitchDelayedRoutine(_delay));
    }

    public void SwitchDelayed()
    {
        StartCoroutine(SwitchDelayedRoutine(targetScene, _delay));
    }

    public void SwitchDelayed(float delay)
    {
        StartCoroutine(SwitchDelayedRoutine(targetScene, delay));
    }

    public void SwitchDelayed(string scene)
    {
        StartCoroutine(SwitchDelayedRoutine(scene, _delay));
    }

    public void SwitchDelayed(string scene, float delay)
    {
        StartCoroutine(SwitchDelayedRoutine(scene, delay));
    }

    private IEnumerator SwitchDelayedRoutine(string scene, float delay)
    {
        Debug.Log($"Started waiting for {delay} seconds ...");
        yield return new WaitForSeconds (delay);

        SceneManager.LoadScene(scene);
    }
}

So this script allows you to switch delayed to another scene configured in targetScene after delay seconds. If you enable autoSwitch the routine will start right away, otherwise you can trigger it at any time via calling SwitchDelayed .

Put this in your Well Done scene and then simply only do

public void LoadNextScene()
{   
    SceneManager.LoadScene("Well done");
}

in your original script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM