Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 patodl2701 · Jun 04, 2021 at 01:54 PM · editorinheritancereferencing

Inheritance in unity editor question

Hey everyone! this is my first post in this forum so i don't know how to properly ask here, please bear with me haha.

I've recently realised that unity editor keeps old references of changed classes.

i.e. If I have a class with one exposed reference to an object and i decide to erase it, I can lately undo that change in code and the reference is kept in the editor.

So I want to ask, is it posible to do it with inheritance?

i.e If i have a base monobehaviour class full of references set on a GO, but later I want to create some other classes that inherits from it and reasign one of them to that GO, is it posible to keep its references, or even other classes references to that object? maybe a tool or an option to do that?

Thanks in advance!

alt text

alt text

help1.png (72.6 kB)
help2.png (88.1 kB)
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
2
Best Answer

Answer by Bunny83 · Jun 04, 2021 at 02:46 PM

So if I understood you correctly you have a base class that you attached to a gameobject and that is serialized and later you want to replace that component with a derived version? That's actually possible ^^.


A MonoBehaviour component in Unity represents several things at once. First of all there's of course the managed class you created. Besides that there's a native representation on the C++ side and that's also where the serialized data is stored. Whenever your scripts are recompiled, the managed side is completely destroyed and rebuild from the serialized data. You can actually exchange the managed class in the inspector. This can be done by switching the inspector into debug mode (rightclick the context menu at the top of the inspector and chose Debug). Now you should see the top field that is called "script" will become editable. It's actually the reference to the MonoScript instance / your script file that this component represents. You can simply drag a different MonoBehaviour script onto that field and the component would essentially "transform" into that new class. Keep in mind to switch the inspector back to normal.


However if you talk about changing the type of a custom serializable class that is serialized inline inside a MonoBehaviour, that's actually not really possible. First of all plain custom serializable classes inside MonoBehaviours do not support inheritance out-of-the-box. You can use the new SerializeReference attribute on a field. By doing this Unity does actually support inheritance and polymorphism, however it does not have any built-in way to specify the actual type you want to use. So you need to create some editor scripts to actually handle this.

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 patodl2701 · Jun 04, 2021 at 07:23 PM 0
Share

It was a bit hard for me to explain the topic 'cause i thought it was something quite complicated, i didn't know that debug mode existed, your comment saved me from a big waste of time!

thank youu!

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

162 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 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 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 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 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 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

Deriving class that derives from MonoBehaviour not showing in editor... 2 Answers

Script component in Editor vs. runtime-dependent inheritance 0 Answers

CustomEditor and inherited components 1 Answer

Copy reference from one component to another derived component., 2 Answers

Modifying/Referencing (Inherited?) Variables within Cinemachine VirtualCamera? 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