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
0
Question by SweatyChair · Aug 17, 2016 at 04:38 PM · inheritancemonobehaviourinstanceserializableclass object

Assigning Serializable child class, not visible in Editor

I have a Person MonoBehaviour class:

 public class Person : MonoBehaviour {
     public string name;
     public Animal pet;
 }

An serializable abstract Animal class:

 [System.Serializable]
 public abstract class Animal {
     public string name;
     public virtual void Run () {}
 }

Also a Cat class inherited from Animal

 [System.Serializable]
 public class Cat : Animal {
     public string catType;
     public override void Run () {Debug.Log ("Cat Run");}
 }

Then in editor I cannot directly assign the pet to a person: Screenshot 1


However, if I specifically assign Cat to Person, it works:

 public class Person : MonoBehaviour {
     public string name;
     public Cat pet;
 }

Screenshot 2

But that isn't what I want: e.g. Tom always has Cat as pet, while Peter can have Dog

The only way I can think of is making Animal a MonoBehaviour, create a prefab, attach the Cat to the prefab, and finally drag the prefab to Person.

This is also not preferable because Animal doesn't need to be MonoBehaviour and this can create a lot of prefabs.

Can this be solved by using custom editor? So I can drag the Cat script to Person, then the editor know the pet is a Cat type and display the Cat variables, etc.

screen-shot-2016-08-17-at-85236-pm.png (12.4 kB)
screen-shot-2016-08-17-at-92236-pm.png (15.2 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
1

Answer by Glurth · Aug 17, 2016 at 05:07 PM

You can define a "custom property drawer" for the Animal Class, which should then show up the in default editor for your Person::MonoBehavior. (note: a "custom property drawer", is different from a "custom editor"- the custom property drawer defines how a "field", of the specified class, is to be drawn.)

A custom editor WOULD also work, but you would have to redo the editor code that "reads" and displays the Animal, for every monobehavior that uses the Animal class.

Alternatively, you can also simply make Animal NOT abstract; and return some default values, rather than have virtual functions. This option will NOT show additional fields that Cat has, so probably not what you want.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to store data in script and attach it later? 0 Answers

C# Inheritance, base class attributes, override and null object 1 Answer

MonoBehaviour Inheritance, Singletons and Generics 1 Answer

Issues Inheriting classes and monoBehaviour 1 Answer

Is there a way to show Monobehaviours class properties inside another Monobehaviour class in the Unity inspector? 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