c# - How to stop a if for a second before executing the rest of the commands -


i been working on script pause menu , don't know how make stop before start executing next line code, ask because execute "if" multiple times, because detects i'm still pressing cancel button. (i use c# , work on unity 5) thanks

 using unityengine; using system.collections;  public class menupausa : monobehaviour {  public gameobject menupausa;  private bool pausamenu = false;  // use initialization void start () {  }  // update called once per frame void update () {     if (input.getbuttondown("cancel") || pausamenu == false) {         menupausa.setactive (true);         time.timescale = 0;         waitforseconds(1);          pausamenu = true;     } else {         menupausa.setactive (false);         time.timescale = 1;         invoke("waiting",0.3f);     } } 

}

i use coroutine kinda this:

bool paused = false;  void update () {     if (paused) // exit method if paused         return;      if (input.getbuttondown("pausebutton"))          startcoroutine(onpause()); // pause script here }  // loops until cancel button pressed // sets pause flag  ienumerator onpause () {     paused = true;      while (paused == true) // infinite loop while paused     {         if (input.getbuttondown("cancel")) // until cancel button pressed         {             paused = false;         }         yield return null; // continue processing on next frame, processing resume in loop until "cancel" button pressed     } } 

this "pauses" , resumes individual script. other scripts continue executing update methods. pause frame rate independent methods (i.e. physics), set time.timescale = 0f when pausing , 1f when unpaused. if pausing other scripts desired , they're dependent upon frame rate (i.e. update methods) use global pause flag instead of local paused variable used in example , check see if flag set in each update method, in example.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -