How can I stop custom mouse cursor from reverting back to standard cursor when new Scene loads?
I have a custom cursor that works perfectly inside the Unity editor but when I build the game and run it in a browser on a PC or mac, the cursor is there on the first scene but disappears when switching scenes.
My script is included below. This script is attached to an empty Gameobject that I have at the top of each scene, though I have tried deleting it from all scenes except the first. I've also tried the code in Start() and Awake() - not Update() because I don't want it to happen every frame.
It really feels like some Unity code that I don't know about is turning my cursor off between scenes or something. I'm using 5.4.1
I'm using these settings on the sprite:
Texture Type: cursor Wrap Mode: Clamp Filter Mode: Point (No Filter)
using UnityEngine; using System.Collections;
public class CustomCursor : MonoBehaviour {
public Texture2D curTexture;
public Vector2 hotSpot;
public CursorMode curMode = CursorMode.Auto;
void Start () {
Cursor.SetCursor(curTexture, hotSpot, curMode);
curTexture.Resize(512, 512);
}
Answer by justDeek · Mar 17, 2017 at 11:02 PM
Everytime you switch scenes, all Objects in the previous scene get destroyed. That's why you have to mark Objects you want not to be destroyed with "DontDestroyOnLoad" in the Awake() or Start() function.
So in the Start() function, add:
DontDestroyOnLoad(this.gameObject);
and then this GameObject stays the way it is persistent through all scenes. But make sure that this GameObject isn't a child of another one, because all parents of this Object will also persist, wich is something you might not want.
Answer by Nedgoom · Mar 18, 2017 at 09:12 AM
Hey, thanks, that's a useful tip that I never knew about - however, I have just realised that there is a totally simple way of doing what I wanted to do in:
Unity - Edit - Project Settings - Player - there you have an option to add your own custom cursor!
Thanks :)