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);

}
}

Hey (not sure why I didn't get an update message for your reply) it seems that Unity have replaced some of the components used since the last time I was involved in any part of networking so I don't think I can help without Googling myself. However, the principal should be the same. I believe the error you are making is trying to delete the object on the client, rather than have the client in question send a message to the host telling it to delete the object. https://docs.unity3d.com/ScriptReference/Networking.NetworkClient.Send.html

1 Answer

1

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);

}