Unity Community


Results 1 to 15 of 15

  1. Location
    Dhaka , Bangladesh
    Posts
    37

    how to pause and Resume in a Game

    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. Posts
    108
    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. Location
    Dhaka , Bangladesh
    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. Location
    Cambodia
    Posts
    582
    I've tried Input.GetMouseButtonDown(0) but still no solution !

    Is there any other solution ?
    You can try Input.GetButtonDown ("Fire1")


  5. Location
    Dhaka , Bangladesh
    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. Posts
    578
    i use time scale to pause my game

    http://unity3d.com/support/documenta...timeScale.html

    when you click the pause button, time scale = 0

    when you click it again time scale = 1
    3d Modeler, Animator, and Developer

    Download Bird Poop for for Free! Click Below!
    Bird Poop Android!
    Bird Poop iOS!

    Online Portfolio at www.patsommer.com


  7. Posts
    578
    sorry i think i misunderstood what your issue was,

    it "looks" like it should work, but obviously isnt 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;
    }

    }
    3d Modeler, Animator, and Developer

    Download Bird Poop for for Free! Click Below!
    Bird Poop Android!
    Bird Poop iOS!

    Online Portfolio at www.patsommer.com


  8. Posts
    578
    also your Update is not capital
    3d Modeler, Animator, and Developer

    Download Bird Poop for for Free! Click Below!
    Bird Poop Android!
    Bird Poop iOS!

    Online Portfolio at www.patsommer.com


  9. Location
    Dhaka , Bangladesh
    Posts
    37
    void update(){

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

    }


    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. Posts
    578
    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
    3d Modeler, Animator, and Developer

    Download Bird Poop for for Free! Click Below!
    Bird Poop Android!
    Bird Poop iOS!

    Online Portfolio at www.patsommer.com


  11. Location
    PA, US
    Posts
    1,134
    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 by KingCharizard; 11-17-2011 at 09:10 AM.


  12. Location
    Dhaka , Bangladesh
    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. Location
    UK
    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. Location
    Dhaka , Bangladesh
    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. Location
    UK
    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.

Posting Permissions

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