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 Zackattak01 · Sep 10, 2017 at 07:13 PM · cursorvisibilitylock-cursoronkeydown

How can I change cursor visibility and cursor lock state when I press tab then revert it back when I press tab again.

In my game I have an inventory system toggle-able by tab. What I also want to happen when I press tab is for the cursor to become visible and movable so I can move items around my inventory with out pressing escape then when I press tab I want the cursor to be locked and invisible just like it started.

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
Best Answer

Answer by HammerCar · Sep 10, 2017 at 08:05 PM

What you are looking for is Cursor.lockState and Cursor.visible.

https://docs.unity3d.com/ScriptReference/Cursor-lockState.html https://docs.unity3d.com/ScriptReference/Cursor-visible.html

 void OpenInventory () 
 {
      Cursor.lockState = CursorLockMode.None;
      Cursor.visible = true;
 }
 
 void CloseInventory () 
 {
      Cursor.lockState = CursorLockMode.Locked;
      Cursor.visible = false;
 }

And pressing escape to see the cursor only works in the editor not in a build.

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 Zackattak01 · Sep 10, 2017 at 08:40 PM 0
Share

So using you answer up above I tried to write a script but when I get into the game and press TAB my inventory opens, I see the cursor, but then it disappears. $$anonymous$$y script is on my main camera. Also if it helps I tried it in both build mode and in unity.

I will include my script below

 using UnityEngine;
 using System.Collections;
 
 public class cursorScript : $$anonymous$$onoBehaviour
 {
     private bool inventoryIsOpen;
     void Start()
     {
         inventoryIsOpen = false;
     }
     
 
     void Update()
     {
 
         
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Tab))
         {
             if(inventoryIsOpen == false)
             {
                 OpenInventory();
             }else if(inventoryIsOpen == true)
                 {
                      CloseInventory();
                 }
         }
 
     }
 
     void OpenInventory()
     {
         Cursor.lockState = CursorLock$$anonymous$$ode.None;
         Cursor.visible = true;
         inventoryIsOpen = true;
     }
 
     void CloseInventory()
     {
         Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
         Cursor.visible = false;
         inventoryIsOpen = false;
     }
 
 }
avatar image HammerCar Zackattak01 · Sep 10, 2017 at 09:02 PM 0
Share

I tested the code you gave me and it worked as intended. If you mean that the cursor disappears right after you see it you might have something setting Cursor.visible to false.

avatar image Zackattak01 HammerCar · Sep 10, 2017 at 09:57 PM 0
Share

Okay thanks for your time. I deter$$anonymous$$ed that it was a setting on the first person prefab (which I was using) to lock the camera now the script works fine. Again thank you for your time.

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

70 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

Related Questions

Trouble with unlocking cursor 1 Answer

Cursor.LockState = CursorLockMode.Locked doasnt works. 0 Answers

Lock cursor on current position 2 Answers

Issues with mouse input when locking the cursor 0 Answers

Cursor locking and visibility. 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