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 MiniForge · Nov 27, 2014 at 01:47 AM · guimenuboolean

Screen.showCursor = !Screen.showCursor; Not working

I have Screen.showCursor set to false after the level is loaded(Not in this script). However I want the cursor to toggle each time input is received from the escape key. I know my Input.getKey stuff is working correctly because the GUI toggle works fine.

In case its not clear what I am trying to do. The user hits escape to bring up a menu--Then the mouse appears--They hit escape again and the cursor disappears! Not sure why my ! toggles aren't working :/

 using UnityEngine;
 using System.Collections;
 
 public class GuiMenu : MonoBehaviour {
     public bool menuShow = false;
     public int menuX;
     public int menuY;
 
     void Update(){
         if(Input.GetKeyDown("escape")){
             menuShow = !menuShow;
             Screen.showCursor = !Screen.showCursor;
             Screen.lockCursor = !Screen.lockCursor;
         }
                 
     }
 
     void OnGUI(){
         if(menuShow){
             GUI.Box(new Rect(menuX, menuY, 100, 160), "Test Pause");
             GUI.BeginGroup(new Rect(menuX, menuY, 100, 160));
             if (GUI.Button(new Rect(10, 10, 80, 40), "Resume")){
                 menuShow = !menuShow;
                 Screen.showCursor = !Screen.showCursor;
                 Screen.lockCursor = !Screen.lockCursor;
             }
             GUI.Button(new Rect(10, 60, 80, 40), "Settings");
             if (GUI.Button(new Rect(10, 110, 80, 40), "Quit")){
                 Application.Quit();
             }
             GUI.EndGroup();
         }
     }
 
 }
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 NaRango4 · Nov 27, 2014 at 07:10 AM 0
Share

did you check with actual Booleans? and one more thing keep

 menuShow = !menuShow;
 Screen.showCursor = !Screen.showCursor;
 Screen.lockCursor = !Screen.lockCursor;

in a separate function, so that you can call it from Update and OnGUI, and use logs to check if you have any extra calls.

2 Replies

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

Answer by ExtremePowers · Nov 27, 2014 at 07:16 AM

I tend to use them like this

          if(Input.GetKeyDown("escape")){
              menuShow = !menuShow;
              Screen.showCursor = menuShow;
              Screen.lockCursor = !menuShow;
          }

Just to make sure that the game creates a bug if one of them wasn't called.

Comment
Add comment · Show 2 · 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 MiniForge · Nov 27, 2014 at 11:21 AM 0
Share

Worked great! And makes more sense to do it this way. Thank you for the idea!

avatar image idiot333 · Mar 21, 2015 at 06:48 PM 0
Share

great smart trick there

avatar image
0

Answer by USMANHEART · Jul 28, 2016 at 01:20 PM

How to use this code in unity 5.2.1?

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

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

an UP-TO-DATE menu tutorial 1 Answer

Pass bool value from multiple objects to one file to check their values? 1 Answer

Appearence of the GUI element after a certain action 1 Answer

GUI circulur menu system 1 Answer

Enable Camera movment on/off with one button 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