tvsA
tvsA

Reputation: 27

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

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);
}

Upvotes: 1

Views: 93

Answers (1)

Declan McKelvey-Hembree
Declan McKelvey-Hembree

Reputation: 1180

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.

Upvotes: 2

Related Questions