+ 2

How can I wait in C#

For a small game I made I want to implement a deathscreen which shows up for 2 seconds. After the 2 Secounds it should load the Main Menu. I know how to load the different scenes but how can I wait until it loads the 2nd screen? public string sceneName1 public string sceneName2 if (collision.tag == "Planet") { SceneManager.LoadScene(sceneName1); //wait for 2 seconds? SceneManager.LoadScene(sceneName2); }

15th May 2020, 11:29 AM
Dennis G
Dennis G - avatar
2 Answers
+ 2
Is that in Unity? If so, you need to start a coroutine, for this, IEnumerator delay(int seconds) { yield return new WaitForSeconds(seconds); } Then you need to call this delay function like that, if (collision.tag == "Planet") // load scene 1 StartCoroutine( delay() ); // or the name of the function in string format StartCoroutine ("delay"); // load scene 2 edit: im not sure about the second way of calling delay function but first must be right. edit2: Actually, for your case, I do not recommend making it wait with hard coded style, instead there is a way of making it like a progress bar that makes it wait as much as computer needs to load that particular scene. For example, for big projects, computer might need like 10 seconds to load the scene but if you make it wait with WaitForSeconds function, it will still require 10 seconds to load the scene and that may cause troubles.
15th May 2020, 12:07 PM
Mustafa K.
Mustafa K. - avatar
+ 1
Thank you very much, and yes it is in Unity
15th May 2020, 7:05 PM
Dennis G
Dennis G - avatar