- Home /
"Prefabs" for classes, scriptableobjects?
I ran into ScriptableObjects recently, but I am not sure if I understand them correctly. In my case I have an Attack class, which holds a timeline with frame data on which hitboxes should be spawned, etc.
I have written an editor extension to manipulate the timeline, add frames, determine framerate, etc. Since this class is not a component, and it simply holds data, I thought it would be handy to let the extend the ScriptableObject class.
Everything went fine until I actually wanted to use the damn thing in runtime. I have a component that wants to get a copy (instance?) of this Attack class, with all the values that have been saved into this Scriptable Object so it can playback the Timeline class during an attack.
However, whenever I try to use ScriptableObject.CreateInstance(), all the fields are null.
Maybe I've misunderstood, and the ScriptableObject is only used to store data, and I should just feed the data manually into a new Attack class instance that I create in the component.
I'd like to simply use this ScriptableObject as some sort of prefab for a class, just being able to get a copy of the Attack with the variables as listed, and then directly reference the instance in the component.
Am I trying to do something unintended, should I somehow use a prefab for this, or did I simply implement the ScriptableObject incorrectly?
Thanks!
Answer by Noktai · Dec 23, 2014 at 12:55 AM
Turns out this works, as for as I've tested for now Attack attackCopy = Instantiate( attack ); //where attack is the reference to the scriptable object
Answer by RudyTheDev · Dec 21, 2014 at 01:15 PM
CreateInstance<>
creates a new instance. It won't have your data. You ought to reference the existing asset with the script like you would a GameObject
. Then access its members. This makes sense because you can have >1 asset with the same ScriptableObject
class type.
As far as I'm aware, there is no easy way to copy the instance, you would probably need to use AssetDatabase
, which is editor-only anyway. ScriptableObject
s are best thought of as data containers that don't get changed at runtime.
Your answer
Follow this Question
Related Questions
How do I instantiate a prefab B in same place prefab A? 1 Answer
How can I set the material on an instance after creating it? 1 Answer
is having a Prefab reference inside a a Scriptable Object legit? 0 Answers
Change material inside instanced prefab, gameobject.renderer is NULL 1 Answer
How can I link a GameObject instance to a script variable in a Prefab using the editor? 1 Answer