- Home /
Storing data - Prefab vs ScriptableObject + Asset
Hi.
I have a class animal which inherits from MonoBehaviour. I need this class to set data for several animals e.g. cat, dog etc with the editor during design time.
Right now I create a GameObject for each animal, add the animal script, set the data for the animal via the editor and save it as a prefab. The prefab is linked by several other GameObjects in the scene just to access the data.
This is working fine so far. But I'm asking myself if it would be a better approach to inherit from ScriptableObject and save this as an asset. Then I'd link these asset instead of a prefab to the other GameObjects which need to access the data.
What is be the better approach of this two? Any pros and cons?
Answer by Bunny83 · Sep 17, 2015 at 09:29 PM
There are no real pros and cons. ScriptableObject is a bit "complicated" to use in contrast to a prefab with a monobehaviour.
The differences are:
ScriptableObject has less overhead since it is just a single class instance. A prefab is always composed of: A GameObject, a Transform component and your script component.
ScriptableObjects have to be created manually as asset. In the past you would have to write editor code to actually instantiate one and save it as asset. Now there is the CreateAssetMenu Attribute which automatically can add a menu item to the "create asset" menu where you can directly create a new asset of your ScriptableObject.
The "CreateAssetMenu" attribute has really improved the usage of ScriptableObjects. So, yes, if you have a lot of animal "types" you want to define using a ScriptableObject might be better. It's always depends on the usecase. The prefab approach has the additional advantage that you could add more components to certain instances and have all grouped together in one asset. With ScriptableObjects you only have that one instance of your class.
Thanks for your detailed answer! I think I'll switch to ScriptableObject in this case then.
But one final question: When I'm using the prefab-solution I can change my script at any time and add or remove public vars etc. The changes are reflected to the prefab automatically. Is this the case using ScriptableObject/Assets, too? Or do I have to create a new asset anytime I'm changing the script.
Yes, you should be able to change / edit your class just like a $$anonymous$$onoBehaviour. In the end Unity uses the same serializer for both classes.
No matter if you use ScriptableObject or $$anonymous$$onoBehaviour you should avoid rena$$anonymous$$g your variables often. Unity introduced the FormerlySerializedAs attribute which is used by the serializer. As the name suggests it is ment to be able to rename a variable and tell the serializer from where it should load the already serialized data.
This is good to know. But why should I avoid to rename my variables during develepment?
Your answer
![](https://koobas.hobune.stream/wayback/20220612034131im_/https://answers.unity.com/themes/thub/images/avi.jpg)