Load Texture2D in Script for mouse cursor

Hi,

I have the Problem that i want to write a c# scipt to change my mouse cursor if it is over some object. So far no problem if i set teh texture manually in the inspector but now i want to change it in that way that the Texture (a png picture with alpha transparancy effect, imported as cursor texture) is loaded automatically in the script, too.

For doing this is added the line
:

void start() {
   cursorTexture = (Texture2D)Resources.Load("Assets/cursors/cursorTake.png");
} 

and also tried it without complete path and without the file extension. But it is never working :frowning:

Does somebody have any idea what i have done wrong?

using UnityEngine;
using System.Collections;

public class cursorControl : MonoBehaviour {
	public Vector2 hotSpot = Vector2.zero;
	private CursorMode cursorMode = CursorMode.Auto;	
	private Texture2D cursorTexture;

	void start() {
		cursorTexture = (Texture2D)Resources.Load("Assets/cursors/cursorTake.png");
	}

	void OnMouseEnter() {
		Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
	}

	void OnMouseExit() {
		Cursor.SetCursor(null, Vector2.zero, cursorMode);
	}
}

Put the cursors folder in a Resources subfolder under Assets/.

  1. You should put your cursor texture in specific folder: Assets/Resources
  2. in Start() method (S - upper case): cursorTexture = Resources.Load(“cursor_tex”) as Texture2D (“cursor_tex” - unique name of your texture)
  3. You should pin this script to some GO and Collider too. Switch on “is trigger” on collider