- Home /
Question by
Ajna · Sep 19, 2014 at 05:45 AM ·
c#texture2dnullreferenceexceptioncursor
Can't load Mouse Cursor texture
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?
Comment
Best Answer
Answer by dmg0600 · Sep 19, 2014 at 10:41 AM
Just change your Update() method for OnGUI(). Unity uses this method to render all GUI mehtods such as the DrawTexture you use for your cursor.
void OnGUI()
{
GUI.DrawTexture(new Rect(Event.current.mousePosition.x - cursorSizeX / 2, Event.current.mousePosition.y - cursorSizeY / 2, cursorSizeX, cursorSizeY), cursor);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Checking for null against Texture2D 0 Answers
Make the cursor hotspot bigger 0 Answers