I have a scene and the scene is a bit different on the client and the server. On the client scene, the SOs are getting loaded just fine automatically (I’m not loading them manual or anything via Resources.Load since they aren’t even in a Resources folder).
On the server scene, it’s a bit different and at first since they weren’t referenced anywhere in the scene they weren’t getting loaded so I created a MonoBehaviour with a list of all the SOs I wanted in the scene and dragged the SOs in that MonoBehaviour that I placed in the scene.
Yet the SOs in that scene still aren’t getting loaded even though I can access their properties and call methods on them, OnEnable is not getting invoked.
ScriptableObjects are just data bags. They don’t have a notion of being enabled or disabled. They just are.
You are of course welcome to put your own .enabled field on them, but that will not cause them to be called by anyone else except you when that field changes!
If you made a MonoBehaviour that contains all your ScriptableObjects, use that MonoBehaviour’s OnEnable() call to do whatever you need with the ScriptableObjects that it has references to.
According to the docs Unity - Scripting API: ScriptableObject they have Awake, OnDestroy, OnEnable, and OnDisable.
Very interesting. I stand corrected. I just made a little test pair of scenes and I am getting Awake(), OnEnable() and OnDisable().
Enclosed find my experiment. Press Space to switch between scenes. Add them both to your editor settings obviously. Do you see the messages too? I am using Unity 5.2.2f1 for this experiment.
NOTE: if you change scene2’s action to loop back to itself (reload scene2) then you DO NOT get the notice that the SO is loaded! Apparently it only unloads itself if it must.
2994972–223089–ScriptableObjectTesting.unitypackage (3.49 KB)
2 Likes