Reputation: 55
In my code I want to implement a timer which gives makes the death wait 2 seconds before it initializes.
void OnCollisionEnter2D(Collision2D other)
{
Die();
}
void Die()
{
Application.LoadLevel(Application.loadedLevel);
}
The death is instant and I want it to wait 2 seconds before it initializes.
Any ideas?
Upvotes: 0
Views: 92
Reputation: 77
Try this:
void OnCollisionEnter2D(Collision2D other)
{
Thread Dying = new Thread(()=>Die());
Dying.Start(); //start a death in new thread so can do other stuff in main thread
}
void Die()
{
Thread.Sleep(2000); //wait for 2 seconds
Application.LoadLevel(Application.loadedLevel);
}
Upvotes: -1
Reputation: 350
If you just want it to happen after two seconds, you can try this -
void OnCollisionEnter2D(Collision2D other)
{
Invoke ("Die", 2.0f);
}
void Die()
{
Application.LoadLevel(Application.loadedLevel);
}
Upvotes: 4
Reputation: 76551
Somewhere initialize a timer with 2000 and define a handler, like this:
//...
Timer tmr = new Timer();
tmr.Interval = 2000; // 20 seconds
tmr.Tick += timerHandler;
tmr.Start(); // The countdown is launched!
//...
private void timerHandler(object sender, EventArgs e) {
//handle death
}
//...
Upvotes: 0