I am using ScriptableObject just to composite some data , in javascript defined like this
class MyDataThing extends ScriptableObject
...
But I noticed that OnEnable is never called. Or it’s called intermittently sometimes. Can’t quite figure out when it gets called or when it’s supposed to get called. I am using CreateInstance(). So also couldn’t figure out what to use as a constructor, so I just wrote an init() method and called it manually.
Now I am thinking- perhaps I should inherit from Object instead of ScriptableObject since I am not hooking into any of the offerings that ScriptableObject has and I am not using Serialization either.
Any pointers , oh wise ones :shock:
Thanks
CreateInstance should just execute the standard constructor:
public MyDataThing()
{
Debug.Log( "Constructor!" );
}
Thanks Emil; yes that is working. Not sure why the docs say OnEnable will get called because it doesn’t.
I searched the wiki and it looks like ScriptableObject is mainly used for Editor extensions and automation.
For just runtime compositing of data, would you recommend inheriting from Object or ScriptableObject? Which do you use for your middleware coding?
Your classes should only extend another class if you need to inherit functionality or make use of polymorphism. Most of my classes are either inherited from other classes I wrote or they’re not explicitly inheriting from anything (all classes automatically inherits from object though - not to be confused with Object).
OK sounds good. I am familiar with O-O programming concepts The Unity docs for ScriptableObject say:
which is actually what confused me! I will just inherit from object instead of ScriptableObject.
But it looks like he was trying to inherit functionality, i.e. OnEnable, OnDisable. I’m having the same problem and now have to write Init / Deinit functions also. In fact I’ve found that OnEnable is called using the exact same code in the Win 2.5 beta. But it isn’t called in Unity iPhone. I haven’t tried it in normal Unity 2 on Mac.
– Brian
I inherit from ScriptableObject so that the instance can be viewed in the Unity inspector for debugging.
I did notice that after creating an instance with ‘new’, results in Start, OnGUI, and Update are not being called.
No events are called this way either:
_myScriptableObject = (MyScriptableObject)ScriptableObject.CreateInstance("MyScriptableObject");
I’ll dig deeper into the forums…
doh…
It’s because ScriptableObjects don’t have those events. I’ll have to use MonoBehavior with AddComponent after all.