Change cursor when hover over object with Tag.

Hi. Thank you for your time.

So I want to switch between two different cursors depending on the tag the cursor hovers over. My own script just won’t do the trick for some reason. Can someone help me out, please?

using UnityEngine;
using System.Collections;

public class Cursor : MonoBehaviour {

	public Texture cursorTextureNormal;
	public Texture cursorTextureNo;
	private bool opTag = false;
	private Ray ray;
	private RaycastHit;

	
	void Start() 
	{
		Screen.showCursor = false;
	}
	

	void OnGUI()
	{
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		Vector3 mousePos = Input.mousePosition;

		if(Physics.Raycast(ray, out hit))
		{
		if(hit.collider.gameObject.tag == "grond")
		{
		opTag = true;
		}

			if(opTag = true)
			{
		Rect pos = new Rect(mousePos.x, Screen.height - mousePos.y, cursorTextureNormal.width, cursorTextureNormal.height);
				GUI.Label(pos, cursorTextureNormal);
	
			}
		else{
			Rect pis = new Rect(mousePos.x, Screen.height - mousePos.y, cursorTextureNo.width, cursorTextureNo.height);
			GUI.Label(pis, cursorTextureNo);
		}
	}
	}
	}
}

Is there a reason that you’re using a GUI label instead of Cursor.SetCursor? Are you also certain that you have the tag value correct (i.e. perhaps you mean “ground” instead of “grond”)? Also, try moving your Raycast code to the Update loop instead of the OnGUI loop. Finally, I’d do something like this (may not work; typed here in the comment field).

void Start()
{
    SetCursorTexture(cursorTextureNormal);
}

void Update()
{
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
        bool useNormalCursorTexture = hit.collider.gameObject.tag.Equals("grond");
        SetCursorTexture(useNormalCursorTexture ? cursorTextureNormal : cursorTextureNo);
    }
}

void SetCursorTexture(Texture tex)
{
    Cursor.SetCursor(tex, Vector2.zero, CursorMode.Auto);
}