Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ken · Jan 15, 2011 at 01:38 AM · unpause

Unpausing Game with GUI

I currently have this code in my OnGUI portion of a script. The game pauses just fine when I click the button the first time, but then won't unpause upon pressing the button again. What am I missing?

    if(GUI.Button(Rect(300,5,305,25),btnTexture) && !paused)
{
Debug.Log("HIT1");
    Time.timeScale=0;
    btnTexture=playTexture;
    paused=true;
}
else if(GUI.Button(Rect(300,5,305,25),btnTexture))
{
Debug.Log("HIT2");
    Time.timeScale=1;
    btnTexture=pauseTexture;
    paused=false;
}

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Jan 15, 2011 at 01:47 AM 0
Share

Are you seeing HIT2 in the console?

avatar image Ken · Jan 15, 2011 at 01:52 AM 0
Share

Changed up the ordering as suggested but didn't change anything. Hit2 still not showing up in the console. I have the script this is in attached to a sphere in-game... does that kill GUI responsiveness?

avatar image DaveA · Jan 15, 2011 at 01:56 AM 0
Share

See comment below

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by DaveA · Jan 15, 2011 at 01:45 AM

I would rearrange it a bit:

private var paused = false; ....

 if(GUI.Button(Rect(300,5,305,25),btnTexture))
 {
   if (!paused)
   {
 Debug.Log("HIT1");
     Time.timeScale=0;
     btnTexture=playTexture;
     paused=true;
   }
   else
   {
 Debug.Log("HIT2");
     Time.timeScale=1;
     btnTexture=pauseTexture;
     paused=false;
   }
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Jan 15, 2011 at 01:57 AM 0
Share

$$anonymous$$ake sure you declare 'paused' outside of your function so it persists between calls.

avatar image Ken · Jan 15, 2011 at 02:00 AM 0
Share

Paused already declared outside the function. Thanks for the attempt though :)

avatar image Ken · Jan 15, 2011 at 02:03 AM 0
Share

Found the problem. $$anonymous$$ade a mistake when I updated the code with your fix and made part of it unreachable. Works great now!

avatar image
1

Answer by Jessy · Jan 15, 2011 at 02:02 AM

The button is only registered as pressed once in OnGUI, so your else if can never fire. Instead, do this:

Edit: I was AFK and DaveA answered with the same code. This code looks formatted, though. ;-)

function OnGUI () {
    if (GUI.Button(Rect(300,5,305,25), btnTexture) {
        if (paused) {
            Debug.Log("Unpaused");
            btnTexture = pauseTexture;
            Time.timeScale = 1;
            paused = false;
        }
        else {
            Debug.Log("Paused");
            btnTexture = playTexture;
            Time.timeScale = 0;
            paused = true;
        }
    }
}
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ChrisSch · Apr 29, 2013 at 06:33 PM

Thank you all of you this helped me too. ^_^ I'm making a tower defense game and needed a pause for when the build menu is open and this did the trick.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Pause Button Script 4 Answers

Sound on pause it doesn't works. 2 Answers

How do I pause and unpause only some GameObjects at a time 1 Answer

Pausing and unpausing the game with one button 2 Answers

In game pause menu 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges