Unity Community


Results 1 to 16 of 16

  1. Posts
    413

    How to pause your game?

    Hi, I was wondering if there was a way to pause you game that does NOT involve setting the time scale to zero? I would imagine that it has to be possible since you can pause in the editor.


  2. Posts
    185
    it's easy really. say I have these scripts.

    Player.js
    MainControl.js
    Enemy.js

    they should speak for themselves, player is the player, enemy is the enemy, and maincontrol is the control of all things that don't relate to other scripts, useful for global variables and such.

    in maincontrol, set a variable isPaused, and make it global.

    then in the other scripts, in the Update functions, make an if statement to check if the isPaused variable in MainControl.js is set to true or false, if it's set to false then run the script, if not then the script wont run (for example, Player.js, the player wouldn't be able to move his character or anything).
    I'm currently employed as a freelancer. At the age of 18 there's not much I can do for work, so if you want something done, you can hit me up at my website! I freelance in web scripting (website creation, and if you have a design in mind I can create that too possibly).

    Also you can visit my website for all things regarding my games creation progress. I make them with Unity3D (of course :P) and I'm fairly new but I'm learning!

    http://akigames.net


  3. Posts
    413
    I did think about doing that, but I have so many scripts running that it doesn't quite seem efficient. Is there really no way to just write one script that pauses all scripts and animations?


  4. Posts
    151
    You could edit the timeScale and set it to 0, which would stop the excecution of most things.


  5. Posts
    185
    he said he didn't want to use timescale.

    that's the only thing I can think of asdf1234. and how many scripts do you have? I highly doubt adding it to your scripts would cause a performance delay or inefficiency, especially considering it can read through the if statement in like a millisecond or less maybe even. if it's set to true, it won't run anything in the if statement, so it completely skips running every line of code in the statement (in this case, your character control, enemy control, etc), if set to false it will run all that just as it normally would.
    I'm currently employed as a freelancer. At the age of 18 there's not much I can do for work, so if you want something done, you can hit me up at my website! I freelance in web scripting (website creation, and if you have a design in mind I can create that too possibly).

    Also you can visit my website for all things regarding my games creation progress. I make them with Unity3D (of course :P) and I'm fairly new but I'm learning!

    http://akigames.net


  6. Location
    Gold Coast, Australia
    Posts
    3,593
    Quote Originally Posted by Akinon93 View Post
    especially considering it can read through the if statement in like a millisecond or less maybe even
    Really? A millisecond?

    http://pastebin.com/hPZ8KFxa

    Code:  
    1. Simple 'if (boolean)' speed test: 49997853
    2. 100,000,000 iterations in 576ms
    3. 173,611 its/ms

    Remembering of course that I'm also:
    • Looping.
    • Reading from a large array.
    • Increasing a counter.


    All in under 6ns.

    So yeah, only off by a few orders of magnitude :P


  7. Posts
    185
    lol well I've never looked it up but I knew that computers can read a single if statement really fast, fast enough that it shouldn't make much of a difference.
    I'm currently employed as a freelancer. At the age of 18 there's not much I can do for work, so if you want something done, you can hit me up at my website! I freelance in web scripting (website creation, and if you have a design in mind I can create that too possibly).

    Also you can visit my website for all things regarding my games creation progress. I make them with Unity3D (of course :P) and I'm fairly new but I'm learning!

    http://akigames.net


  8. Posts
    413
    Ok so i found out that OnApplicationPause() is what causes the game to pause in the editor or if "Run In Background" is not checked on a standalone/webplayer. So, the problem here is: #1Can you call this ingame? #2 if so, is there a way to incorporate a pause menu even if this is on (which is doubtful).


  9. Location
    New Jersey
    Posts
    2,161
    What I do is make a manager class with a static variable isPaused, and check it in every script.
    Code:  
    1. function Update () {
    2.  
    3.     if (Manager.isPaused)
    4.         return;
    5.  
    6.     Logic ();
    7.  
    8. }


  10. Posts
    413
    Quote Originally Posted by Dman View Post
    What I do is make a manager class with a static variable isPaused, and check it in every script.
    Code:  
    1. function Update () {
    2.  
    3.     if (Manager.isPaused)
    4.         return;
    5.  
    6.     Logic ();
    7.  
    8. }
    I'm confused on what this script does. I do know a good amount of javascript but im a bit confused with what you put there. Do i have to reference that in all scripts, or does that one take care of everything?


  11. Posts
    185
    I think he said what I said before. make a static variable isPaused, and check if it's true or not in all your scripts before running them in the Update function.
    I'm currently employed as a freelancer. At the age of 18 there's not much I can do for work, so if you want something done, you can hit me up at my website! I freelance in web scripting (website creation, and if you have a design in mind I can create that too possibly).

    Also you can visit my website for all things regarding my games creation progress. I make them with Unity3D (of course :P) and I'm fairly new but I'm learning!

    http://akigames.net


  12. Location
    Singapore
    Posts
    1,347
    Basically you will need a state manager to set the state of the game, pause/playing or other running states and these are global/static variables because you only 1 instance of these variables So if somewhere in your script, something calls your state manager tell his the game is paused the other scripts will have to also check with state manager if game is running or in a pause state.

    Thats what the script Dman just posted. Basically you add that codes to every of your object in your game to pause running if game is paused.
    I don't break rules. I bend them.


  13. Location
    Gold Coast, Australia
    Posts
    3,593
    Quote Originally Posted by Akinon93 View Post
    lol well I've never looked it up but I knew that computers can read a single if statement really fast, fast enough that it shouldn't make much of a difference.
    Of course, that's 100% correct.

    The mistake was thinking that 1ms was a small amount of time, in real time software like games 1ms is significant (assuming 30FPS, you've only got ~ 30 of 'em to work with each frame!)


  14. Posts
    413
    So how can I enable/disable a script from inside the script i'm enabling/disabling? Wouldn't it not be able to re-enable itself since its been turned off? or do i have to make another script to control the script i'm turning on and off?


  15. Posts
    185
    refer to my post above.

    Enemy.js
    Player.js
    Control.js

    control could be something like this.

    Code:  
    1. static var isPaused = false;
    2.  
    3. function Update(){
    4. //do the code that should only be done if the game is paused.
    5. }}

    and Player.js might look like this, for example.

    Code:  
    1.  
    2. function Update(){
    3. if(!isPaused){
    4. //Move around scripts, weapon firing scripts, anything that should happen when the game is -not- paused.
    5. }}

    the enemy script would look the same as the player script.

    if you want to pause the game, you'd just do this.

    Code:  
    1. Control.isPaused = true;

    to unpause, just set to false.
    I'm currently employed as a freelancer. At the age of 18 there's not much I can do for work, so if you want something done, you can hit me up at my website! I freelance in web scripting (website creation, and if you have a design in mind I can create that too possibly).

    Also you can visit my website for all things regarding my games creation progress. I make them with Unity3D (of course :P) and I'm fairly new but I'm learning!

    http://akigames.net


  16. Posts
    413
    Ok, thank you everybody I'll try adding a control script for pausing that i reference in the scripts

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •