Author Topic: Pause Button  (Read 9865 times)

dev01

  • Guest
Pause Button
« on: February 21, 2013, 03:06:26 PM »
How can I implement a game pause using an image button I have?
I cant seem to find a script in the examples that will let me pause and un-pause my game.

I am no scripter! thats why I use ngui
so an example script on how to do this would be appreciated.
Thanks in advance!

lime-green.at

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 60
    • View Profile
Re: Pause Button
« Reply #1 on: February 21, 2013, 06:23:30 PM »
Set Time.timeScale to zero, on unpause just reset it to the value before you set it. (I got some problems with setting it to 1)
http://docs.unity3d.com/Documentation/ScriptReference/Time-timeScale.html

tydygunn

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 20
    • View Profile
Re: Pause Button
« Reply #2 on: February 21, 2013, 07:55:54 PM »
There are much better ways to do this, but if you're not a scripter then setting the timescale to 0 is probably your best bet.

C#
  1. void OnClick()
  2. {
  3.      if(Time.timeScale == 1) Time.timeScale = 0;
  4.      else Time.timeScale = 1;
  5. }

If you make a C# script with that function and toss it on your button it should work. Like I said though, this could mess you up because Unity's Time is used for a lot of different things and stopping it's flow could stop your other scripts from functioning if they reference Time.time or anything similar.

redhawk

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 34
    • View Profile
Re: Pause Button
« Reply #3 on: February 22, 2013, 05:09:35 PM »
I've used the below code inside JavaScript in unity.  Haven't attached it to an NGUI object yet.  Also, in addition to the below, I set my Start function to Time.timeScale=1;

  1. if(Time.timeScale==0)
  2. {
  3. //I also changed my button to white for not paused
  4.    Time.timeScale=1;
  5. }
  6. else
  7. {
  8. //I also changed the color of my Button to Red for paused
  9.    Time.timeScale=0;
  10. }
  11.