Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by gyrosp · Sep 17, 2015 at 09:10 PM · prefabsscriptableobjectdata storage

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gyrosp · Sep 18, 2015 at 07:27 AM 0
Share

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.

avatar image Bunny83 gyrosp · Sep 18, 2015 at 10:16 AM 1
Share

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.

avatar image gyrosp Bunny83 · Sep 18, 2015 at 10:28 AM 0
Share

This is good to know. But why should I avoid to rename my variables during develepment?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Store Playerdata in Scriptable Object 2 Answers

saving prefabs in the onDestroy method 0 Answers

Find Source of Prefab Instance in Editor? 1 Answer

getting a list of objects from a data path 0 Answers

Some data of scriptable object resets when Unity Editor restarts 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges