I am trying to wrap my head around ScriptableObjects and their possibilities. My goal is to have a Mouse Manager that keeps track of the mouse position, objects it’s hovering over, and other behaviors. I want this manager to be read by anything that wants to know whenever it wants to know it.
I know I could make a MonoBehavior script that has ScriptableObject Variables (like a Vector3) that it keeps updated, but that requires me to add the manager to each scene manually. And then I would need to include each variable as a reference into any other asset that needs to read it.
Is there a better way to do this? Have the manager as a ScriptableObject, each attribute also a ScriptableObject, and just reference the manager in whatever script needs access to it?
I can see why you’re attracted to ScriptableObject for a MouseManager. The problem with a MouseManager as a ScriptableObject though, is that ScriptableObjects don’t receive Unity callbacks such as Start() and Update(). So in order to update the mouse manager with new mouse data, you would need a MonoBehaviour somewhere that is reading the mouse position in Update anyway.
There are some ways to improve upon this. You can use a singleton pattern to make accessing the MouseManager from other scripts easier(Google for “Unity Singleton pattern” and you will find hundreds of examples).
You can also call DontDestroyOnLoad on the MouseManager’s Start method. Then you would only need to include it in your first scene and it would persist between scene loads.
1 Like
Think of ScriptableObjects as “predefined bags of data” for your game. Mouse input is dynamic. Perhaps you could have ScriptableObjects that configure the sensitivity of your mouse, but it would be a bit misleading to put dynamic information into a ScriptableObject at runtime.
1 Like
Thanks guys, that helped!