Load scene in Unity with a delay using C#

Hello programmers, In this article, I will show how to load a scene in Unity with a delay using C# programming.

Let’s get started with the building process.

 

To load the scene we need to add UnityEngine.SceneManagement to the code. That will define the SceneManager.

After that add the [SerializeField] with private float delayBeforeLoading and give the delay value in seconds.

Add private string sceneNameToLoad this will take the input of the Scene name.

In Update function run the if () condition comparing with (delayBeforeLoading), when its true call the SceneManager.LoadScene and scene will be loaded after the delay time.

using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadAfterTime : MonoBehaviour
{
    [SerializeField]
    private float delayBeforeLoading = 10f;
    [SerializeField]
    private string sceneNameToLoad;
    private float timeElapsed;

    void Update()
    {
        timeElapsed += Time.deltaTime;

        if (timeElapsed > delayBeforeLoading)
        {
            SceneManager.LoadScene(sceneNameToLoad);
        }
    }
}

 

:- After you run the above program, the next scene will be loaded after a time delay of 10 seconds.

Leave a Reply

Your email address will not be published.