3
public class Cursor : MonoBehaviour 
{ 
     public Texture2D cursor; 
     public int cursorSizeX = 16; // default 
     public int cursorSizeY = 16; // default 

// Use this for initialization 
void Start () 
{ 
     Object temp = Resources.Load("Textures/CR_Cursor (Custom)"); 

     if (temp == null) 
     Debug.Log("Load Object Fail"); 

     cursor = (Texture2D)Resources.Load("Textures/CR_Cursor (Custom)"); 

     if (cursor == null) 
     Debug.Log("Load Cursor Fail"); 

     Screen.showCursor = false; 
} 

// Update is called once per frame 
void Update () 
{ 
     GUI.DrawTexture(new Rect(Event.current.mousePosition.x - cursorSizeX / 2,      Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), cursor); 
} 

no matter where I instantiate the cursor im sill getting NullReferenceException: Object reference not set to an instance of an object Cursor.Update (), what I am missing?

2 Answers 2

2

you should put this line

GUI.DrawTexture(new Rect(Event.current.mousePosition.x - cursorSizeX / 2,      Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), cursor); 

inside of OnGUI() and not in Update

void OnGUI(){

GUI.DrawTexture(new Rect(Event.current.mousePosition.x - cursorSizeX / 2,      Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), cursor); 

}

here is a chart concerning the lifecycle of script

Scripting Lifecycle

Sign up to request clarification or add additional context in comments.

2 Comments

That did it. Even so I see the mouse blinking sometimes, specially at the beginning of the scene. Any way to avoid it? I should use no Update() function then? If I did use one, OnGUI would be executed peviously or later than Update?
i put up a script lifecycle chart and im pretty sure you can set the graphic for the mouse cursor in player setting somewhere
0

Ensure that you have change the texture properties as following in Inspector window,

  • Texture Type: Cursor
  • Alpha Is Transparent: True
  • Read/Write: True
  • Generate Mip Maps: False

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.