c# - How make the script wait/sleep in a simple way in unity -


how can put between textui.text = .... sleep function, wait 3 seconds between each phrase?

public text guessui; public text textui;  [...truncated...]  textui.text = "welcome number wizard!"; textui.text = ("the highest number can pick " + max); textui.text = ("the lowest number can pick " + min); 

i tried various things didn't worked, such this:

textui.text = "welcome number wizard!"; yield waitforseconds (3); textui.text = ("the highest number can pick " + max); yield waitforseconds (3); textui.text = ("the lowest number can pick " + min); 

in bash be:

echo "welcome number wizard!" sleep 3 echo "the highest number can pick 1000" sleep 3 ..... 

but can't figured out how in unity c#

there many ways wait in unity. simple think it's worth covering ways these:

1.with coroutine , waitforseconds.

the far simplest way. put code need wait time in coroutine function can wait waitforseconds. note in coroutine function, call function startcoroutine(yourfunction).

example below rotate 90 deg, wait 4 seconds, rotate 40 deg , wait 2 seconds, , rotate rotate 20 deg.

void start() {     startcoroutine(waiter()); }  ienumerator waiter() {     //rotate 90 deg     transform.rotate(new vector3(90, 0, 0), space.world);      //wait 4 seconds     yield return new waitforseconds(4);      //rotate 40 deg     transform.rotate(new vector3(40, 0, 0), space.world);      //wait 2 seconds     yield return new waitforseconds(2);      //rotate 20 deg     transform.rotate(new vector3(20, 0, 0), space.world); } 

2.with coroutine , waitforsecondsrealtime.

the difference between waitforseconds , waitforsecondsrealtime waitforsecondsrealtime using unscaled time wait means when pausing game time.timescale, waitforsecondsrealtime function not affected waitforseconds would.

void start() {     startcoroutine(waiter()); }  ienumerator waiter() {     //rotate 90 deg     transform.rotate(new vector3(90, 0, 0), space.world);      //wait 4 seconds     yield return new waitforsecondsrealtime(4);      //rotate 40 deg     transform.rotate(new vector3(40, 0, 0), space.world);      //wait 2 seconds     yield return new waitforsecondsrealtime(2);      //rotate 20 deg     transform.rotate(new vector3(20, 0, 0), space.world); } 

wait , still able see how long have waited:

3.with coroutine , incrementing variable every frame time.deltatime.

a example of when need timer display on screen how time has waited. timer.

it's when want interrupt wait/sleep boolean variable when true. yield break; can used.

bool quit = false;  void start() {     startcoroutine(waiter()); }  ienumerator waiter() {     float counter = 0;     //rotate 90 deg     transform.rotate(new vector3(90, 0, 0), space.world);      //wait 4 seconds     float waittime = 4;     while (counter < waittime)     {         //increment timer until counter >= waittime         counter += time.deltatime;         debug.log("we have waited for: " + counter + " seconds");         //wait frame unity doesn't freeze         //check if want quit function         if (quit)         {             //quit function             yield break;         }         yield return null;     }      //rotate 40 deg     transform.rotate(new vector3(40, 0, 0), space.world);      //wait 2 seconds     waittime = 2;     //reset counter     counter = 0;     while (counter < waittime)     {         //increment timer until counter >= waittime         counter += time.deltatime;         debug.log("we have waited for: " + counter + " seconds");         //check if want quit function         if (quit)         {             //quit function             yield break;         }         //wait frame unity doesn't freeze         yield return null;     }      //rotate 20 deg     transform.rotate(new vector3(20, 0, 0), space.world); } 

you can still simplify moving while loop coroutine function , yielding , still able see counting , interrupt counter.

bool quit = false;  void start() {     startcoroutine(waiter()); }  ienumerator waiter() {     //rotate 90 deg     transform.rotate(new vector3(90, 0, 0), space.world);      //wait 4 seconds     float waittime = 4;     yield return wait(waittime);      //rotate 40 deg     transform.rotate(new vector3(40, 0, 0), space.world);      //wait 2 seconds     waittime = 2;     yield return wait(waittime);      //rotate 20 deg     transform.rotate(new vector3(20, 0, 0), space.world); }  ienumerator wait(float waittime) {     float counter = 0;      while (counter < waittime)     {         //increment timer until counter >= waittime         counter += time.deltatime;         debug.log("we have waited for: " + counter + " seconds");         if (quit)         {             //quit function             yield break;         }         //wait frame unity doesn't freeze         yield return null;     } } 

wait/sleep until variable changes or equals value:

4.with coroutine , waituntil function:

wait until condition becomes true. example function waits player's score 100 loads next level.

float playerscore = 0; int nextscene = 0;  void start() {     startcoroutine(sceneloader()); }  ienumerator sceneloader() {     debug.log("waiting player score >=100 ");     yield return new waituntil(() => playerscore >= 10);     debug.log("player score >=100. loading next leve");      //increment , load next scene     nextscene++;     scenemanager.loadscene(nextscene); } 

5.with coroutine , waitwhile function.

wait while condition true. example when want exit app when escape key pressed.

void start() {     startcoroutine(inputwaiter()); }  ienumerator inputwaiter() {     debug.log("waiting exit button pressed");     yield return new waitwhile(() => !input.getkeydown(keycode.escape));     debug.log("exit button has been pressed. leaving application");      //exit program     quit(); }  void quit() {     #if unity_editor     unityeditor.editorapplication.isplaying = false;     #else     application.quit();     #endif } 

6.with invoke function:

you can call tell unity call function in future. when call invoke function, can pass in time wait before calling function second parameter. example below call feeddog() function after 5 seconds invoke called.

void start() {     invoke("feeddog", 5);     debug.log("will feed dog after 5 seconds"); }  void feeddog() {     debug.log("now feeding dog"); } 

7.with update() function , time.deltatime.

it's #3 except not use coroutine. uses update function.

the problem requires many variables won't run every time once when timer on after wait.

float timer = 0; bool timerreached = false;  void update() {     if (!timerreached)         timer += time.deltatime;      if (!timerreached && timer > 5)     {         debug.log("done waiting");         feeddog();          //set false don't run again         timerreached = true;     } }  void feeddog() {     debug.log("now feeding dog"); } 

there still other ways wait in unity should know ones mentioned above makes easier make games in unity. when use each 1 depends on circumstances.

for particular issue, solution:

ienumerator showtextfuntion() {     textui.text = "welcome number wizard!";     yield return new waitforseconds(3f);     textui.text = ("the highest number can pick " + max);     yield return new waitforseconds(3f);     textui.text = ("the lowest number can pick " + min); } 

and call/start coroutine function start or update function, call

startcoroutine (showtextfuntion()); 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -