I want to delay 3 sec to load next level after the collision in C#
I need to delay the load new level by a few second. Please help.This is the script I need to use:
public class myscore : MonoBehaviour {
void OnCollisionEnter2D(Collision2D col){
Debug.Log ("Collision");
if (col.gameObject.tag == "Player") {
score = score + 50;
Destroy (gameObject);
*Here I need to wait for 3 sec then it should load the next level*
SceneManager.LoadScene(nextLevel);
} }
Answer by PrisVas · Jun 28, 2016 at 07:36 PM
Use StartCoroutine:
*Here I need to wait for 3 sec then it should load the next level*
StartCoroutine(WaitForIt(3.0F));
//SceneManager.LoadScene(nextLevel);
IEnumerator WaitForIt(float waitTime) {
yield return new WaitForSeconds(waitTime);
SceneManager.LoadScene(nextLevel);
}
Hi, still it doesn't work. If I write this code then my score is not updating and next level is not loading :( . Please help. Thanks
I see. It's not working because you are destroying the object before it finishes the others instructions.
You can put the OnCollisionEnter2D and WaitForIt on the player script, so you can destroy this object with no problem. Or you can hide the object while waiting for the 3 secs.
Hi, my score is updating and next level is loading in the OnCollisionEnter2D method with my old code but when I add this code then nothing is working. I tried several ways but it didn't work :(. right now it looks like this :
void OnCollisionEnter2D(Collision2D col){
Debug.Log ("Collision");
if (col.gameObject.tag == "Enemy") {
score = score + 50;
Destroy (gameObject);
StartCoroutine (WaitForIt (3.0F));
}
}
IEnumerator WaitForIt(float waitTime) {
yield return new WaitForSeconds(waitTime);
Scene$$anonymous$$anager.LoadScene(nextLevel);
}
Yours works because it is instant. But you want to wait 3 seconds. so you can't destroy the object and and do something 3 seconds later. An exemple: Imagine you and your mother in the same room, so you say "mom, bring me some water, please" and just after you say: "bring me some cookies too". But then imagine you say: "mom, bring me some water, please" and wait 3 seconds, say: "bring me some cookies too". She wont hear you, because she's already out of the room. So you have to destroy the object 3 seconds later. And I guess, if you are loading a new scene, you dont even need to destroy it, just load the nextLevel.
Comment the Destroy (gameObject); and will be fine.
But, if you REALLY need to disapear with this object, don't destroy it, do this:
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("Collision");
if (col.gameObject.tag == "Player")
{
score = score + 50;
//Destroy(gameObject);
gameObject.GetComponent<SpriteRenderer>().enabled = false;
gameObject.GetComponent<Collider2D>().enabled = false;
StartCoroutine(WaitForIt(3.0F));
}
}
Your answer
Follow this Question
Related Questions
Instantiate an object and destroy it after given time 2 Answers
Weather Timer (Beginner to scripting) (In multiplayer) 0 Answers
How to use IEnumerator? Pls help! 1 Answer
Wait in unity(coroutine not working) 0 Answers
how to change a value over time 1 Answer