Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 shinytwig · Jun 07, 2012 at 10:31 PM · menuescape

Escape menu script problem

The script I've made here is for an in-game escape menu, it has 2 buttons, Resume and Quit, quit leads to the main menu and resume un-pauses the game. When you first hit escape it disables the mouselook script for the player and sets the timescale to 0, when you hit resume it's supposed to set timescale to 1 and enable the mouselook script again.

Here's the script:

 function Update ()
 {
     if(Input.GetKeyDown("escape"))
     {
         if(Time.timeScale == 0)
         {
             Time.timeScale = 1;
         }
         else
         {
             Time.timeScale = 0;
         }
     }
 }
 
 function OnGUI()
 {
     if(Time.timeScale == 0)
     {
         ButtonGUI();
         script = GetComponent(MouseLook);
         script.enabled = false;
     }
 }
 
 function ButtonGUI()
 {
     GUI.Label (Rect (100, 25, 200, 60), "PAUSED");
     
     if (GUI.Button (Rect (100, 55, 100, 30), "Resume"))
     {
         Time.timeScale = 1;
         script = GetComponent(MouseLook);
         script.enabled = true;
     }
     
     if (GUI.Button (Rect (100, 95, 100, 30), "Quit"))
     {
         Application.LoadLevel(0);
     }
 }

What did I do to make it not work?

Comment
Add comment · Show 1
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 Bunny83 · Jun 07, 2012 at 10:34 PM 0
Share

Uhhm, i guess you forget to tell use the actual problem... ?!?

What happens? What's wrong? What does it do what you didn't expect?

1 Reply

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

Answer by Bunny83 · Jun 07, 2012 at 10:51 PM

Some things that might cause problems:

  • You toggle the timescale when ever you press escape, but you don't handle the enable / disable of your script at this place.

  • Disabling the script in OnGUI doesn't make much sense. This will disable the script 2 times per frame...

  • Make sure this script is attached to the same object that contains the Mouselook script or GetComponent will fail. If they should be on different objects, use a public variable to access the mouselook script.

I would do it like this:

 var script : MouseLook;
 
 
 function Start()
 {
     // Since there is propably just one MouseLook script, FindObjectsOfType will
     // find the instance no matter to which gameobject it's attacht to.
     / We do it in start because FindObjectsOfType / GetComponent is a slow function
     script = FindObjectsOfType(MouseLook) as MouseLook;
 }
 
 function SetPause(state : Boolean)
 {
     if (state)  // We want to pause the game
     {
         Time.timeScale = 0.0;
         script.enabled = false;
     }
     else
     {
         Time.timeScale = 1.0;
         script.enabled = true;
     }
 }
 
 function Update ()
 {
     if(Input.GetKeyDown("escape"))
     {
         SetPause(Time.timeScale == 1.0); // toggle
     }
 }
 
 function OnGUI()
 {
     if(Time.timeScale == 0)
     {
         ButtonGUI();
     }
 }
 
 function ButtonGUI()
 {
     GUI.Label (Rect (100, 25, 200, 60), "PAUSED");
 
     if (GUI.Button (Rect (100, 55, 100, 30), "Resume"))
     {
         SetPause(false); // exit "pause" mode
     }
 
     if (GUI.Button (Rect (100, 95, 100, 30), "Quit"))
     {
         Application.LoadLevel(0);
     }
 }
Comment
Add comment · Show 1 · 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 shinytwig · Jun 07, 2012 at 11:46 PM 0
Share

Sorry, the problem was that if you hit escape again or hit resume the mouselook script wouldn't re enable. I read through your answer and saw that I put the enable and disable in the wrong place, I went back and added an if statement at the top and added another function to call that when I paused it then called that function in the OnGUI function, it works now, thanks for your answer!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The Escape Menu opens more than once 1 Answer

Deactivate UI windows in order when "Escape" button is pressed. 1 Answer

Unity 3D C# - Scripts ot true false not working 2 Answers

input question 1 Answer

Hold the esc button for a pause menu 1 Answer


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