Any Way to Delay Scene Change?

Currently I have this code:

// I have 'CheckNotes' triggered every time a note is collected.

void CheckNotes ()
    {
        if (notes == 0)
        {
            starText.text = "0%";
        }
        if (notes == 1)
        {
            starText.text = "33%";
        }
        if (notes == 2)
        {
            starText.text = "66%";
        }
        if (notes == 3)
        {
            starText.text = "100%";
            winText.text = "Level   Complete!";
            SceneManager.LoadScene("Level2")
        }

Everything works, but as soon as the third note is collected it immediately loads the next scene/level. If I remove ā€˜SceneManager.LoadScene(ā€œLevel2ā€)ā€™ it displayes 100% and ā€œLevel Completedā€. I was wondering if there was a way to display ā€œLevel Completedā€ and then a couple seconds later load the next scene. So the player has time to see ā€œLevel Completedā€ before the scene changes. I am new to Unity, any help is appreciated, I havenā€™t been able to find a working answer on any other posts. Thanks. (C#)

You would Invoke an event to run after x seconds, something like this:

using UnityEngine;
using UnityEngine.SceneManagement;

public class MyClass : MonoBehaviour {

    // Time in seconds to delay (five seconds for example):
    public float delayTime = 5f;

    int notes = 0;

    void OnTriggerEnter2D(Collider2D other){
        if(notes == 3){
            Invoke("DelayedAction", delayTime);
        }
    }

    void DelayedAction(){
        SceneManager.LoadScene("Level2");
    }

}
4 Likes

You could start a timer, that counts down for a set amount of time, then loads the scene when it reaches 0,

Shown below.

// I have 'CheckNotes' triggered every time a note is collected.
    public float timer;
    private bool starttimer = false;
    void CheckNotes()
    {
        if (notes == 0)
        {
            starText.text = "0%";
        }
        if (notes == 1)
        {
            starText.text = "33%";
        }
        if (notes == 2)
        {
            starText.text = "66%";
        }
        if (notes == 3_
            {
            starText.text = "100%";
            winText.text = "Level   Complete!";
            starttimer = true;

            if (starttimer)
            {
                timer -= 1.0f * Time.deltaTime;

                if (timer <= 0.0f)
                {

                    SceneManager.LoadScene("Level2");
                    starttimer = false;
                   
                }
            }
        }

Thanks, that seems a lot easier than any other solutions I have found. Iā€™m testing it nowā€¦

It works, thanks a lot!

I am guessing you used my exampleā€¦ If so your welcome!

1 Like

Yes, I used your example. Thanks.

1 Like

Yes, I used your example. Thanks.

I was scrolling through google and found your answer and it helped me a lot!v Thanks!

1 Like

Might be a little late to this but I get this message in the console. Trying to Invoke method: SceneChange.DelayedAction couldnā€™t be called.

Please donā€™t necro-post for a simple runtime error.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you donā€™t have to stop your progress and fiddle around with the forum.

Remember: NOBODY here memorizes error codes. Thatā€™s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

If you just need a general ā€œdo something later please,ā€ I always use this pattern:

I use my CallAfterDelay class for delayed action.

https://gist.github.com/kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e

See usage notes at bottom below gist code.