Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Lapraniteon · Oct 10, 2019 at 09:56 AM · scripting problemuicursor

Cursor.visible doesn't hide cursor a second time.

Hi!
I am trying to create an fps game which hides the cursor hides in-game but appears in the pause menu. I am currently using Cursor.visible to hide the cursor every time I close the pause menu and make it appear every time I open the pause menu. Everything seems to work fine when opening the pause menu; The cursor appears and I can click and all that. But when I close the pause menu the cursor does not hide. This is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class pause : MonoBehaviour
 {
     
     public GameObject pauseMenuCanvas;
     public bool gamePaused = false;
     public bool cursorVisible = false;
     
     void Start()
     {
         pauseMenuCanvas = GameObject.Find("PauseMenu");
         pauseMenuCanvas.SetActive(false);
     }
 
 
     void Update()
     {
         if (Input.GetKeyDown("escape")) {
             gamePaused = !gamePaused;
             cursorVisible = !cursorVisible;
         }
         
         if (gamePaused == true) {
             Time.timeScale = 0.0f;
             pauseMenuCanvas.SetActive(true);
         }
         
         if (gamePaused == false) {
             Time.timeScale = 1.0f;
             pauseMenuCanvas.SetActive(false);
         }
         
         Cursor.visible = cursorVisible;
     }
 }

It may not look very well made because I am fairly new to scripting with C#.
Any help would be greatly appreciated!!

Comment
Add comment · Show 2
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 KaspianR · Oct 10, 2019 at 12:07 PM 0
Share

Are you using Unity’s prebuilt character controller or a custom one? It is possible the controller might handle this too and therefore be overriding your code?

avatar image sbsmith · Oct 11, 2019 at 03:10 AM 0
Share

Are you testing this in the Editor or testing it in a build? Cursor stuff in the Editor is sometimes a bit weird. $$anonymous$$ake a build of your game, try it and see if you still have the cursor problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lgarczyn · Oct 12, 2019 at 02:57 PM

This is pretty weird.

I have one suggestion, which is to avoid setting the state of the cursor and the timescale every frame. Your code could look something like this:

     void Update()
     {
         if (Input.GetKeyDown("escape")) {
             gamePaused = !gamePaused;

             if (gamePaused) {
                 Debug.Log("Pausing");
                 Time.timeScale = 0.0f;
             } else {
                 Debug.Log("Unpausing")
                 Time.timeScale = 1.0f;
             }
             pauseMenuCanvas.SetActive(gamePaused);
             Cursor.visible = gamePaused;
         }
     }


If this doesn't solve your problem, try to check if any other script is changing the cursor visibility. You could add a line to check for it, like this:

     void Update()
     {
         Debug.Log("Cursor visibility before: " + Cursor.visible);
         if (Input.GetKeyDown("escape")) {
             [...]
         }
         Debug.Log("Cursor visibility after: " + Cursor.visible);
     }


It will flood your logs, but you can simply explore them later. To reduce the amount of logs, reduce framerate, play the game frame by frame, or add a counter to only display this every few frames.

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

260 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 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 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 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 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 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 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 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 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

Cursor Toggle in Menu (UI) 1 Answer

How to click and drag images into multiple slots? 0 Answers

How to get the current cursor texture? 0 Answers

How to link buttons to individual levels within the same scene? 1 Answer

My UI Buttons stopped working 0 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