Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by Xarbrough · Aug 27, 2015 at 12:36 AM · componentscriptableobjecttype

Serialize a reference to MonoBehaviour type in ScriptableObject

Is there a way to store a reference to a MonoBehaviour type in a ScriptableObject asset, so that I can AddComponent said type at runtime? There used to be an overload of AddComponent that takes a string "classname", but it's deprecated now, so how can I get a reference to the type I need from my ScriptableObject asset?

I have a set of ScriptableObject levels, which I use to load certain data like "level time limit" etc. At runtime, I want to add specific behaviour to my existing level class. I know this sounds, like all specific functions should inherit from "BaseLevel" and then override their behaviour, which would work, but then I would have to create as many prefabs as I have behaviours. I already have my set of ScriptableObjects, so I would like to actually reference those scripts in my SOs, instead of creating a new set of prefabs and then linking both.

Thanks!

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

2 Replies

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

Answer by Xarbrough · Sep 20, 2015 at 08:06 AM

Sorry to answer my own question, but I found the perfect solution here: ClassTypeReference by Rotorz

@IgorAherne: Thanks for your answer. Interesting reads and generally good information, but I didn't really get anything working until I found Rotorz' solution, which did exactly what I wanted.

Comment
Add comment · Show 1 · 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 huulong · Dec 03, 2021 at 12:14 AM 0
Share

2021 update: after following links to more recent repositories successively, I found the modern equivalent: https://github.com/SolidAlloy/ClassTypeReference-for-Unity

avatar image
1

Answer by IgorAherne · Aug 27, 2015 at 12:45 AM

Short answer: http://answers.unity3d.com/questions/292634/how-to-reference-monobehaviour-in-scriptableobject.html

Hardcore (but possible way): If you want to serialize unity's internal stuff you will have to go through tornment of BinaryFormatter (2-3 days of learning)

I found an excellent post about Surrogates which are used to serialize unity's Unserializable Vector3s and transforms, etc.

If you spend time reading it's invaluable here you go:

1) Binary Serialization intro: https://www.youtube.com/watch?v=Y2P_9s3KhdU

2) Read the answer and he also links a different thread, read what his comments are saying there: http://answers.unity3d.com/questions/990996/serializable-lists-items-become-nulls.html

3) he links this thread: http://answers.unity3d.com/questions/967840/saving-your-scene-and-location-in-game.html

After that, you will be able to flush binary data into scriptable object and restore it backwards to interact with the scene. The data you cant serialze via binary serializator(Vector3s, Transform, GameObjects ) will have work arrounds shown in steps 2) and 3)

Comment
Add comment · 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

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

27 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

Related Questions

Getting all component types (even those not on the scene) 1 Answer

Disable an unknown type of component at Runtime 2 Answers

Declare Component type in GetComponent 2 Answers

Easy? Convert System.Type to UnityEngine.Component 3 Answers

Accessing components via script and assigning randomized values to their parameters? 1 Answer


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