For all Unity developers and developers-to-be, both beginners and professionals!

Report article RSS Feed How to hide the cursor?

How to hide the default system cursor in-game when it's not needed (in some platformers, etc.).

Posted by feillyne on Oct 24th, 2010
Basic Other.

It's rather simple.

1. Create a script that and name it e.g. HideCursorScript, then edit it and paste the following code:

javascript code:
function Update () {
//hide the cursor (at the beginning of the game)
Screen.showCursor = false;
}

2. Attach your script to a game object, whether your character or simply the main camera - by dragging the script onto the object.

3. Then, to display the cursor, e.g. when pressing ESCAPE key to see the pause menu, you may use the code below:

javascript code:
var script = GetComponent("HideCursorScript");
script.enabled = false;

The full code that can be found in CarEdu source:

javascript code:
function Update () {

if(Input.GetKey("escape")) {
    //pause the game
    Time.timeScale = 0;
    //show the pause menu
    var script3 = GetComponent("PauseMenuScript");
    script3.enabled = true;
    //disable the cursor hiding script
    var script4 = GetComponent("HideCursorScript");
    script4.enabled = false;
    }
}

Location: Scripts/JavaScripts/UpdateScript.js
Also must be dragged onto the object.

4. In the resume button script there must be some code to activate the hiding script cursor back, see below:

javascript code:
var script4 = GetComponent("HideCursorScript");
script4.enabled = true;

The full code can be found here: Scripts/JavaScripts/PauseMenuScript.js
The script must be dragged on the object, too.

In CarEdu, all three scripts were dragged onto PauseMenuCamera object in CompleteScene scene.

Also note that HideCursorScript and UpdateScript are both enabled while PauseMenuScript is disabled by default. It's also very important, as enabling the pause menu script would show the pause menu immediately upon launching the game.

Summing everything up, notice these lines:
Screen.showCursor = false;
This line makes the cursor hidden.
Screen.showCursor = true;
And this one makes it shown.

These, along with enabling/disabling the short HideCursorScript script at the beginning are the core of hiding the cursor.

Post comment Comments
Guest
Guest Sep 5 2013, 3:04am says:

This comment is currently awaiting admin approval, join now to view.

Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Established
Oct 31, 2010
Privacy
Public
Subscription
Open to all members
Homepage
Unity3d.com
Contact
Send Message
Email
Members Only
Membership
Join this group
Group Watch
Track this group
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Games
CarEdu
CarEdu Indie Single & Multiplayer Racing
Related Engines
Unity
Unity Commercial Released May 30, 2005
Related Groups
Indie Devs
Indie Devs Hobbies & Interests group with 1,098 members
Unity devs
Unity devs Hobbies & Interests group with 1,192 members