Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to manually pause/unpause by script?

Discussion in 'Scripting' started by leegod, Sep 18, 2013.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,335
    So Unity editor has button of [Pause] beside of [Play] button.

    I want that pause button's effect.

    How can this be done via scripting?


    So my problem is after I save my game via serialization solution in asset store,

    Unity gui's label text being broken.

    This well fixed by pause game, and then unpause game via unity editor's button.
     
  2. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Not sure if there is a way to replicate the pause function exactly.

    This thread here might help you.

    I myself use the key press P and then set speeds to 0 on objects or something to that effect.
     
  3. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    Just write:

    Code (csharp):
    1.  
    2. EditorApplication.isPaused = true;
    3.  
    Obviously you can only do this in an editor script - it makes no sense to try to do this from a non-editor script because it's an editor-only feature. You can control the play button state in the same way, and also execute single frame steps - read the EditorApplication documentation for details on these.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    from runtime scripts you can use Debug.Break() to pause execution
     
    zeimhall, suIly, TheDevloper and 5 others like this.
  5. Shinugami

    Shinugami

    Joined:
    Oct 17, 2012
    Posts:
    54
    I know you want the editor.Pause but is the Timescale of the game any good to you? I used it in the following example:

    I had a problem where I was debugging which AI would get caught on obstacles. I made a script that samples their position numerous times and provides an average distance between all the distance elements. This meant that if the average distance between the Vector3 values in this array was too low; the AI is stuck.

    So I made this condition play a sound effect to get my attention so I could do other stuff instead of watch them run around for ages. Even with the alarm, sometimes by the time I found the AI, it had become unstuck due to the Timescale being so fast. This is because I had increased the timescale to speed up the time until they got stuck because I was trying to improve their code.

    So I wanted the editor to pause. Seeing that it required extra effort I realized I am already using the Timescale function and so why not just use this so that the game is slowed down to an almost standstill. So I did. It's not a pause but it's kind of better than a pause because if the AI was not really stuck but just triggered an alarm, I have the logic still running so that it can get back to work.

    If the AI manages to unstuck itself then I have the code check that average distance between positions and if it's greater than the alarm value it will restore the timescale back to maximum so that it speeds up the time until the AI get stuck again.
     
    nandanramesh13 likes this.
  6. diliupg

    diliupg

    Joined:
    Jan 23, 2018
    Posts:
    45
    The easiest way is to add Debug.Break() and call it from a keypress. You can pause after the frame start and before the frame ends. Somewhere in-between. ( @danielwilkins, is that correct? )
     
    Last edited: Nov 11, 2021
  7. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    this guy has a tutorial on that

     
  8. harko12

    harko12

    Joined:
    Jul 6, 2012
    Posts:
    10
    I ran into an issue where I wanted to be able to step though what was happening frame by frame, but I didn't want a debugger break, because that changed the behavior of the code. So I put this code into where I thought the problem was beginning


    #if UNITY_EDITOR
    UnityEditor.EditorApplication.isPaused = true;
    #endif

    That clicked the pause button on the editor as soon as my code was triggered, letting me step through frame by frame and look at values in the editor. Naturally this wouldn't work in a build, but if you need to pause the editor and don't have catlike reflexes to hit the pause button manually, this is one way to do it.
     
  9. danielwilkins

    danielwilkins

    Joined:
    Feb 27, 2019
    Posts:
    17
    Incorrect. The pause happens at the end of the frame.
     
    M0r5e80 likes this.
  10. The_MrX_

    The_MrX_

    Joined:
    Aug 25, 2019
    Posts:
    12
    Since this seems to high in google search for pausing/unpausing the editor.

    you can use Ctrl+Shift+P during play to pause/unpause. Incase that's all you want.
     
    sidney7111 likes this.
  11. sidney7111

    sidney7111

    Joined:
    Jun 28, 2020
    Posts:
    7
    Ok, It's really useful ;
    and I think it works as well
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. UnityEditor.EditorApplication.isPaused = true;
    3. #endif
     
    zelderus and FlightFight like this.
  12. Allesster

    Allesster

    Joined:
    Aug 1, 2013
    Posts:
    13
    Code (CSharp):
    1. Debug.Break();
     
    Protozoaire likes this.
  13. valentin56610

    valentin56610

    Joined:
    Jan 22, 2019
    Posts:
    146
    This does not work anymore with Unity 2022
    At least not on Mac (CMD + SHIFT + P)
    It used to work in Unity 2021