Cursor.SetCursor problem

Hey,

I will be brief… how does Cursor.SetCursor works exactly? I’ve read the documentation here, here and here, but it works completely unreliably.

First I tried setting the cursor once and see what happens:

	public Texture2D tex;
	public Vector2 offset;

	private void Start()
	{
		Cursor.SetCursor(tex, offset, CursorMode.Auto);
	}

Result? Nothing happens. Then looking through some questions on forums and stuff it occurred to me that maybe, for some reason, it needs to be set every frame as lots of people suggest using things like OnMouseOver, so I tried the following:

	public Texture2D tex;
	public Vector2 offset;

	private void Update()
	{
		Cursor.SetCursor(tex, offset, CursorMode.Auto);
	}

Which, actually, worked! Cool awesome so easy! That is… until you try to actually work with the cursor in interesting ways… for example, in my game, when equipping ranged weapons, I want the cursor to be a crosshair, or melee weapons, a sword, or mouse over menu items, a pointer… so I tried the following code:

	public int cursorOverride = -1;
	public int defaultCursorIndex;

	public Texture2D cursorMenu;
	public Texture2D cursorSword;
	private Vector2 defaultCursorOffset = new Vector2(0, 0);
	public Texture2D cursorCrosshair;
	private Vector2 cursorCrosshairOffset = new Vector2(32, 32);

	private void Update()
	{
		if (cursorOverride == -1)
			SetCursor(defaultCursorIndex);
		else
			SetCursor(cursorOverride);
	}

	private void SetCursor(int index)
	{
		if (index == 0)
		{
			Cursor.SetCursor(cursorMenu, defaultCursorOffset, CursorMode.Auto);
		}
		else if (index == 1)
		{
			Cursor.SetCursor(cursorSword, defaultCursorOffset, CursorMode.Auto);
		}
		else if (index == 2)
		{
			Cursor.SetCursor(cursorCrosshair, cursorCrosshairOffset, CursorMode.Auto);
		}
	}

Its extremely simple logic, and the logic is correct… the appropriate IF ELSE line gets called when I want it to… BUT… the cursor doesnt change! In Unity Editor, I have to click outside of the play area a few times, and get back to the play area and SOMETIMES the mouse is updated with the new texture… sometimes it doesnt… when exporting the project for web player (which is my target platform) nothing happens at all (both on Mac and PC)… really, I don’t know what Im doing wrong… it seems to be so simple, how can it be bugging?

Any ideas?

PS: My images are tagged as “Cursor” on the import settings, and I just dragged them to the script to make sure the script knew the images.

Thanks in advance,
Best regards,
Allan

Not sure on how you perform the changing of cursorOverride. But I suspect that is where your problem lies since it is in the update method, if you have a toggle button its more of a hit and miss i.e. updates every frame.

If you simply test the following within the update method:

		if(Input.GetKey("1")){
			cursorOverride = 1;
		}
		
		if(Input.GetKey("2")){
			cursorOverride = 0;
		}

it works with no problems even within browser.

Other classes access this class and change the values both of the default and override variables. I´ve even done a simple “print (override + " " + default)” and always got the expected results. I put a “print(“test1/2/3”)” on the code where it calls Cursor.SetCursor and it also is called exactly when expected to.

The problem is… the cursor does not change, or in the browser, not even appears once, none of them… so if the problem was on how I am handling the override/default variables, at least ONE cursor would show up… but not even that =/

I´ve tested even as simple as this:

private void Update()
{
Cursor.SetCursor(tex, offset, CursorMode.Auto);
}

And it didnt work on the browser as well… does work on Unity Editor, a bit unrealiably (sometimes it takes a lot to show up). Maybe this is a bug? Im not on my work machine right now but I believe I got the latest Unity Pro version installed, not sure on the number right now. Using a Mac OSX 10.8.5 to develop.

PS: Just so you know, I handle these variables at only specific times… like on weapon switch, change the default from sword to crosshair… and on mouse over GUI, change the override to Menu Cursor, on mouse exit change override back to -1… really, theres no secret to it.

PS: Using Unity Pro 4.3.4f1… not that I think that this changes anything… =/

Alright, just tested the exact same build on my friend’s PC, with Unity Pro 4.3.0f4… and it works exactly as supposed to. Definitely there is a bug here…

EDIT: lol… exported a web version through his PC… didnt work on web =( This is getting a little sad

EDIT: Ok… I feel silly… it wasnt working but now it is -.-… ok so I am probably making something wrong… anyway… I will leave the code “for reference”…

EDIT2: Apparently it really does not work on Chrome, on Mac, but works on different browsers (firefox, safari), and apparently is working on all browsers in PC…

Ok, I’ve made the simplest example possible. You can download the project here (188kb) and view the web export here.

The code is nothing more than a counter so you know when the mouse should change, an while(true) changing the mouse cursor every 5 seconds, and 3 different cursors that get selected randomly, but never repeat themselves (if its on cursor 1 it can only select cursor 0 and 2… if on 2, only 1 and 0…)

I am developing on Mac OSX 10.8.5, Unity Pro 4.3.4f1.

Please, if anyone has any ideas and take the time to download and check my files to see if I am possibly doing something wrong, I would be grateful forever.

Thanks for the attention,
Best regards,
Allan

I’ve had some oddities with SetCursor as well.

  • It appears you need to wait a small amount of time after the game loads to set the cursor. I did this in a coroutine Start(). Setting the default cursor in the Project Settings might mitigate this.
  • I had issues when I did the first pass of my implementation where the cursor would not show up. Restarting the Editor seemed to have worked and, in fact, I started a thread about it - http://forum.unity3d.com/threads/190196-Cursor-SetCursor-in-Play-Mode

Have you tried forcing it into software mode for Web Player?

Looks like this is still an issue, in 2019. How sad.

And in 2021 it seems…