Hi Guys, for some reason I can’t find a simple solution to a very simple problem. I just need to change from scene 1 to scene 2 after a timer ends. No jargon! No hacking crap! No trying to get me to visit a site!! Just a C# script for Unity 5.5 that will do the job.
Sorry to be blunt but youtube is full of old videos and kids trying to get you to sub to their channel or direct you to another site that is full of dodgy downloads.
Is there anyone honest who can help me out.
Cheers
Steve
The SceneManager class (link goes to official Unity documentation) is what you want to use to switch to a new scene. The most basic way to use it is by calling SceneManager.LoadScene(scene);
, where scene is either an integer representing the scene’s index in the Build Settings, or a string representing the name of the scene asset.
Either way, you’ll need to add your scenes to the Build Settings to use this (File → Build Settings). You can dig around in the SceneManager documentation for more things you can do with loading scenes, but this’ll serve for now.
To make this happen after a timer ends, you can do this:
public class SceneTimer : MonoBehaviour
{
// can be set in the Inspector for this script
public string sceneName;
// alternatively, you can do something like
// public const string sceneName = "SecondScene";
// time after this script initializes, in seconds,
// that the scene transition will happen
public const float TIME_LIMIT = 5F;
// timer variable
private float timer = 0F;
// alternatively, you can set this in an Awake() function,
// which is automatically called when the script initializes
// automatically called many times every second
void Update()
{
// deltaTime is the time (measured in seconds) since the previous Update step
// it's typically very small, e.g. 1/60th of a second ~= 0.0167F
this.timer += Time.deltaTime;
// check if it's time to switch scenes
if (this.timer >= TIME_LIMIT)
{
SceneManager.LoadScene(sceneName);
}
}
}