Problem with changing the Mouse Cursor

Hi,

i want to change my Mouse Cursor. Because i am new to Unity i searched for a Tutorial and found an not so old Tutorial for Unity 5.

My problem is, that the Method SetCursor() for Cursor is not existent. I already found an old post where it is said that i need to include UnityEngine in my script which is the case. It is till not working.

using UnityEngine;
using System.Collections;

public class Cursor : MonoBehaviour {

    public Texture2D defaultCursor;
    public CursorMode curMode = CursorMode.Auto;
    public Vector2 hotSpot = Vector2.zero;

    void Start () {
        Cursor.SetCursor (defaultCursor, hotSpot, curMode);
    }

}

Error Message: Cursor' does not contain a definition for SetCursor’

I hope anyone can help me.

I could fix it by myself. This was really stupid by me. The name of the class was also ‘Cursor’ so this could not work. Just changed the Classname and now everything is fine.

2 Likes

Thats because there is already a class named Cursor. you named your class the same thing so you have a namespace conflict. When you say Cursor the conpiler don’t know if you mean your Cursor class or the Cursor class in UnityEngine namespace. its assuming you mean yours because you’re calling from inside your class’s scope.

either name your class something else

public class CursorScript: MonoBehaviour

or put it into a namespace and make a full call to the function, explicitly saying you want UnityEngine’s Cursor class, not yours.

using UnityEngine;
using System.Collections;
namespace MyScripts
{
    public class Cursor : MonoBehaviour {
        public Texture2D defaultCursor;
        public CursorMode curMode = CursorMode.Auto;
        public Vector2 hotSpot = Vector2.zero;
        void Start ()
        {
            UnityEngine.Cursor.SetCursor (defaultCursor, hotSpot, curMode);
        }
    }
}

I often run into this similar issue if I specifically want System.Object or UnityEngine.Object. and just being explicit is the clearest route.