- Home /
How do I transition between scenes automatically?
I have code that transitions between scenes when you click the screen, but I would like to change it so it can change automatically after a certain amount of time. What should I add to or remove from this code to make it do that? This code came from a Brackey's Video
Here's the code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelLoader : MonoBehaviour {
public Animator transition;
public float transitionTime = 1f;
void Update()
{
if(Input.GetMouseButtonDown(0))
{
LoadNextLevel();
}
}
public void LoadNextLevel()
{
StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex + 1));
}
IEnumerator LoadLevel(int levelIndex)
{
transition.SetTrigger("Start");
yield return new WaitForSeconds(transitionTime);
SceneManager.LoadScene(levelIndex);
}
}
Answer by Fadrik · Mar 13, 2021 at 10:25 AM
you can add this script in you scene and simply remove the Update function and just add this
void Awake()
{
LoadNextLevel();
}
and by that when the Scene is loaded it instantly start loading the next level after the transitionTime is passed.
and also you can use this instead of using Coroutines
void LoadScene(int sceneIndex)
{
SceneManager.LoadScene(sceneIndex);
}
and call this function from wherever you like with this
Invoke("LoadScene",transitionTime );
Invoke("LoadScene",transitionTime ); has the following errors: (26,9): error CS1001: Identifier expected (26,20): error CS1031: Type expected (26,20): error CS1001: Identifier expected (26,36): error CS1001: Identifier expected How do I fix this?
did you make sure that all the variables used in the Invoke are right in your script the Function Name and transitionTime ?