Home > Net >  Why doesnt my script change the Unity scene as it should?
Why doesnt my script change the Unity scene as it should?

Time:08-07

So i have made a simple code that on click of a button will load a scene in unity, wait for 5 seconds and then switch to another scene. But while it firstly will load a scene, it won't load the second one. If anyone knows what am I doing wrong since I am a huge beginner in unity and coding, please help. <3

public void PickLockClick()
{
    StartCoroutine(PickLockScene());
}


    IEnumerator PickLockScene()
{
    SceneManager.LoadScene(2);
    yield return new WaitForSeconds(5);
    SceneManager.LoadScene(3);
}

CodePudding user response:

This code exists in some initial scene, Scene 1, right?

When you load Scene 2, it replaces Scene 1, so the code in Scene 1 stops executing, including the PickLockScene coroutine! You'll need to start a coroutine inside Scene 2 that waits 5 seconds and then loads Scene 3.

  • Related