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 Phrongorre · Apr 11, 2015 at 02:34 PM · uimenuonclicktroubleshootingscreen.lockcursor

Trouble with Unity 4.6.1f1 Screen.lockCursor

I am trying to create a simple pause menu system (dubbed "pawsMenu" for in-game reasons) through a script attached to an empty GameObject seperate from the menu (a sort of universal GameController object, if you will) that allows for the user to press the "Cancel"/ESC key on the keyboard to simultaneously bring up the pause menu system in question by setting the GameObject it is parented to to be active, and also unlocking the mouse cursor through "Screen.lockCursor = false;". This all works just fine. I have created a function within this code that is called Continue() which essentially undoes the same steps used to bring up the pause menu: setting its parent GameObject to be inactive and re-locking the cursor.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PawsMenu : MonoBehaviour {
 
     public GameObject pawsMenu;
 
     bool lockCursor = true;
     bool showMenu = false;
 
     void Update () {
         pawsMenu.SetActive(showMenu);
         Screen.lockCursor = lockCursor;
 
         if(Input.GetButtonDown("Cancel")) {
             if(showMenu == true && lockCursor == false) {
                 Continue();
             }
             else if(showMenu == false && lockCursor == true) {
                 showMenu = true;
                 lockCursor = false;
             }
         }
     }
 
     public void Continue() {
         showMenu = false;
         lockCursor = true;
     }
 
     public void MainMenu() {
         Application.LoadLevel("MainMenu");
     }
 }

The problem I am running into is this: this Continue() function is normally called through the Unity UI button OnClick() system to close the pause menu in-game in order to continue playing. When the function is called in this way, it does exactly what I want it to do, closing the menu and locking the cursor again. But, if try to add the ability to call that same function from within the code by a press of the "Cancel"/ESC key when the menu is open and the cursor is not locked, in hopes to then close it in the same manner as mentioned previously, the menu will close as desired, but the cursor won't lock and stays visible. I don't understand why there would be a difference between the two ways of calling the same function and why only the line of code referring to the cursor locking would be affected by this change.

If anyone can help at all, I would REALLY appreciate it. Thanks.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AlwaysSunny · Apr 06, 2015 at 05:40 AM

This might be due to setting the active state of this object to false prior to making use of the lockCursor variable. Not sure whether the remainder of that Update function would execute after calling SetActive(false). Try moving the Screen.lockCursor line above the pawsMenu.SetActive line.

I dunno whether this is still a thing or not, but in previous versions, I recall it being necessary to set lock/hide every frame. Certain situations (like changes to application focus) seemed to reset these Screen properties. Just something to watch out for, I doubt it's the issue you're seeing here.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[Partially Solved] OnClick of one button, show different set of buttons. 1 Answer

The button works only after repeated mouse hover. 0 Answers

Making a UI.Button put text into a InputField 1 Answer

New dropdown menu sample 3 Answers

Pause menu works on all scenes but one... 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