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

Post tutorial Report content 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:

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:

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

The full code that can be found in CarEdu source:

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:

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 Sep 5 2013 says:

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

Post a comment

You are not logged in, your comment will be anonymous unless you join the community. Or sign in with your social account:

Oct 31, 2010
Open to all members
Send Message
Join this group
Group watch
Start tracking
Related Games
CarEdu Racing
Related Engines
Unity Commercial Released 2005
Related Groups
Indie Devs
Indie Devs Hobbies & Interests with 1,285 members
Unity Devs
Unity Devs Hobbies & Interests with 1,447 members