- Home /
Timer to Switch Scene, not working, New to Coding
I'm new to coding so I don't know much and having problems with a script I made. I'm trying to make a script that counts down from x amount of seconds (not visible in scene) and, when it reaches 0, switch to a different scene. MonoDevelop give me this error "Error Error: System.ArgumentException: An item with the same key has already been added. (Error: System.ArgumentException) (Assembly-UnityScript)" and below is the code I wrote, I would be overly Thankful if you could tell me where I went wrong and how to fix it
#pragma strict
var seconds : float = 5;
function Update ()
{
seconds -= Time.deltaTime;
if (seconds == 0);
{
Application.LoadLevel("example");
}
}
Answer by iamsidv · Jul 13, 2015 at 01:32 PM
I actually don't find this approach amusing. Since you want the scene to get loaded after x amount of seconds (not visible in scene) and, when it reaches 0, switch to a different scene.
You can try this approach :--
var startTime;
private var timeLimit= 10.0;
function Start(){
startTIme = Time.time;
}
function Update () {
if (Time.time > startTime+timeLimit) {
Application.LoadLevel("example");
}
}
OR this would help you out..
private var timeLimit= 10.0;
function Start(){
Invoke("LoadLevelExample", timeLimit);
}
function LoadLevelExample() {
Application.LoadLevel("example");
}
Hope this helps.
Sorry! I've not been able to access my Unity Account for a while. $$anonymous$$any Thanks for your help!
Answer by BiG · Jul 10, 2015 at 07:28 AM
The semicolon has not to be used in ending if statements' body. Just do:
if (seconds <= 0)
{
Application.LoadLevel("example");
}
It's working a bit better now, but its not switching scenes atall, have you got anything else I can try? $$anonymous$$any Thanks!
@Reubix: try this other approach: http://answers.unity3d.com/questions/30811/change-level-after-10-secs.html It should be more "clean"...
And make sure that the level "example" has been added to the levels' list.
$$anonymous$$ost likely seconds
will never be exactly zero. So you should use
if ( senconds < 0 )
Your answer
Follow this Question
Related Questions
terrain wont come up in scene 0 Answers
Camera not showing in scene view 0 Answers
Codeing Issues making a Timer 1 Answer
Scene restarting problem 0 Answers
Unity running out of memory 1 Answer