- Home /
Timers, LoadLevel
Hi Im trying to load another level after 60 seconds into the game. Im new to unity heres my code so far. Do I need a update() method maybe??
private float countdownTimerStartTime=0;
private int endtime =5;
void OnAwake(){
// record current time for timer to count down from
countdownTimerStartTime = Time.time;
if(countdownTimerStartTime == endtime)
{
Application.LoadLevel(3);
}
}
Answer by Seth-Bergman · May 01, 2012 at 03:43 AM
edit: sorry, you're using c# .. anyway, you could do:
void Awake()
{
StartCoroutine(MyLoadLevel(5.0f));
}
IEnumerator MyLoadLevel(float delay)
{
yield return new WaitForSeconds(delay);
Application.LoadLevel(3);
}
Answer by Bunny83 · May 01, 2012 at 03:51 AM
Ok first you should take a deep look into the scripting reference and maybe read some beginner-tutorials. The Overridable Functions should give you an idea.
Some points about your code:
There's no "OnAwake", it's called just "Awake"
Awake and Start are just being called once. To check a progressing value (Time.time) you have to check it every frame (in Update) or use a coroutine which kind of runs parallel to your other stuff.
The time returned by Time.time is a float (floating point number). It holds numbers with a fractional part. Checking float values for equality doesn't work most the time since 5.000011 is not the same as 5.000012
Your endtime value is an integer value (whole number without fractional part). Mixing floats with ints is also not a good idea. The time is a float, so any other time value should also be a float.
Instead of == you should use >=
If you really want to just load a new level after a fix time without any further condition you could use a coroutine which waits the desired amount of time.
The Start() function can also be a coroutine so you can just do something like this:
private float endtime = 60.0f; // 60 seconds
IEnumerator Start()
{
yield return new WaitForSeconds(endtime);
Application.LoadLevel(3);
}
Sorry, I miss read your question, the best way to solve this is with WaitForSeconds like Bunny83 says.
Thank you all for your comments and help much appreciated. Thats great help worked for me perfect and I learned what I was doing wrong thank you guys :))
Answer by -hiTo- · May 01, 2012 at 04:18 AM
Awake() only fires once, so in order to determine if 60 seconds has gone by, you need to either check all the time, if the time has gone by, in an Update, for example. Or start a timer that will execute commands after a specified time. Coroutines are good, but if you're new, I would suggest working with Invokes instead. They come more natural.
private void Awake()
{
Invoke("StartMyLevel", endTime);
}
private void StartMyLevel()
{
Application.LoadLevel("YourLevel");
}
Your answer
Follow this Question
Related Questions
Delays when move to another scene 1 Answer
Next lvl code help 1 Answer
level load problem 2 Answers
Giant spike with level load causes crash with iphone 1 Answer
Creating a GameObject fails directly after Loading a Level 1 Answer