Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to pause and Resume in a Game

Discussion in 'Scripting' started by masudias, Nov 15, 2011.

  1. masudias

    masudias

    Joined:
    Oct 15, 2011
    Posts:
    37
    Hello,

    I'm new in Unity. I'm in a trouble with pausing my game. I want when mouse is clicked anywhere of the game screen the game will be paused and clicking again will resume it.

    I've tried this code


    void update(){


    if(Input.GetMouseButton(0) mychar.active == true)
    {
    mychar.active=false;
    }

    if(Input.GetMouseButton(0) mychar.active == false)
    {
    mychar.active=true;
    }

    }


    But it is not working as i supposed. The game is being paused when I press the left click , but when i released it resumes again !

    How to get rid of this problem? Is there any other solution or any other function to pause and resume ?

    Thanks in advance,
    _Masudias
     
  2. Jack-Trades

    Jack-Trades

    Joined:
    Apr 5, 2009
    Posts:
    107
    You want Input.GetMouseButtonDown(0) instead of Input.GetMouseButton(0).

    Also
    mychar.active is the same as mychar.active == true
    and
    !mychar.active is the same as mychar.active == fasle
     
  3. masudias

    masudias

    Joined:
    Oct 15, 2011
    Posts:
    37
    Thanks for kind answer .

    I've tried Input.GetMouseButtonDown(0) but still no solution ! :(

    Is there any other solution ?

    Thanks in advance
    _Masudias
     
  4. henry96

    henry96

    Joined:
    Sep 28, 2011
    Posts:
    582
    You can try Input.GetButtonDown ("Fire1")
     
  5. masudias

    masudias

    Joined:
    Oct 15, 2011
    Posts:
    37
    if(Input.GetMouseButtonDown(0))
    {
    this.enabled=false;

    while(true)
    {
    if(Input.GetMouseButtonDown(0)) break;
    }

    this.enabled = true ;
    }


    I've tried this code. But nothing . :(
    Thanks for kind answers . Any help further ? :(

    _Masudias
     
  6. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Posts:
    586
  7. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Posts:
    586
    sorry i think i misunderstood what your issue was,

    it "looks" like it should work, but obviously isnt :D lol try this maybe


    void update(){


    if(Input.GetMouseButton(0) mychar.active == true)
    {
    mychar.active=false;
    }
    else if(Input.GetMouseButton(0) mychar.active == false)
    {
    mychar.active=true;
    }

    }
     
  8. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Posts:
    586
    also your Update is not capital
     
  9. masudias

    masudias

    Joined:
    Oct 15, 2011
    Posts:
    37
    void update(){



    Thanks for your kind answers Pat_sommer :)

    Btw , I wrote that code in Update() Function !

    But here you are missing a point .. i think ... :p

    When you are deactivating a gameobject its update function will not be called next time . So there's no chance to get into that "else if" portion that you mentioned . I tried something that will not finish running Update() function. So i've tried a while() to keep running update() function alive . But this didn't work too . :(

    Any help further ? :(

    Thanks ,
    _Masudias
     
  10. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Posts:
    586
    i thought if a gameobject wasnt active nothing on it worked at all? its only update?

    you have this script attached to the object your deactivating? if so that may be your issue, attach it to another object and declare the object you want to deactivate as a variable

    where are you getting mychar from? are you posting your full script? i dont see you declaring it anywhere
     
  11. kingcharizard

    kingcharizard

    Joined:
    Jun 30, 2011
    Posts:
    1,137
    Do you have a pause scene set up? your game just wont pause and show a save load options screen you have to switch to the Pause Scene,

    if your character doesnt stop moving why dont you try Time.timeScale == 0; that should pause him.

    Also look into using FixedUpdate

    I could be wrong so try it out. I'll look into it more if it doesnt work

    Also THIS may help

    Remember Google is your friend I typed this into google "how to make a pause screen in unity 3d" and found the above wiki answer...
     
    Last edited: Nov 17, 2011
  12. masudias

    masudias

    Joined:
    Oct 15, 2011
    Posts:
    37
    Thanks a lot for kind answers.
    Btw, I've a new problem, How can I disable iPhone touch input during my game is running and enable it again. Any idea?
     
  13. Sock Puppet

    Sock Puppet

    Joined:
    Jan 9, 2011
    Posts:
    77
    The best way I have found to pause a game is to have an update hierarchy.

    In other words don't rely on Update() instead manually call custom update functions on your objects from a root manager class. That way you have complete control, input included, of what is updating and what isn't.

    You can also control what is being updated whilst your game is paused.
     
  14. masudias

    masudias

    Joined:
    Oct 15, 2011
    Posts:
    37
    The Pause/Resume problem has been solved. I've used Time.timescale to do that and it works just fine!
    But now I'm in need of the solution how can i temporarily disable iPhone touch and re enable it again during my game is running.

    Thanks,
    _Masudias.
     
  15. Sock Puppet

    Sock Puppet

    Joined:
    Jan 9, 2011
    Posts:
    77
    A boolean should be all you need. E.g if(!paused) GetInput().

    Time.timeScale = 0 will not pause everything, which I prefer not to rely on it completely and instead manage it with a heirarchy. That way you don't have to have a if(!paused) check on every object that has pause dependant code.