• 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 laoril · Sep 16, 2017 at 12:21 PM · dynamicparent-childcasting

Filling a basic ParentClass list with ChildClass "dynamically"

Hi,

Let me explain what I'm trying to do : I'm trying to create a custom inspector for an Item database which contains a list of items of different classes which all inherit from ParentClass. I want to be able to fill it with any ChildClass I could pass as a paramater, but keeping the attributes of the ChildClass. So if I cast my ChildClass as a ParentClass I cannot access the child attributes.

I have been able to get the Type of ChildClass from a string using a function which looks for the type in all current assemblies like this :

 public Type getTypeFromAssembly(string className)
     {
         Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
         Type myType = null;
         foreach (Assembly assembly in assemblies)
         {
             myType = assembly.GetType(className);
             if (myType != null)
             {
                 break;
             }
         }
         return myType;

The issue is when I want to add it to the list myParentClassList, I can't cast the ChildClass using a Type variable like this :

 string textType = "ResourceItem";
 Type thisType = getTypeFromAssembly(textType);
 object obj = Activator.CreateInstance(thisType);
 myParentClassDatabase.myParentClassList.Add((thisType)obj);

And if I cast it as (ParentClass), attributes from the child class aren't display since Unity can't know which type of child it is.

I know the basics in C# but I'm no expert at all, so please bear with me ;-) I've tried Convert.ChangeType but I'm running into the same problem.

Maybe there is a simple solution for what I'm trying to achieve ? Could anyone give me a hint ? 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

1 Reply

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

Answer by Bunny83 · Sep 16, 2017 at 02:10 PM

The correct way to cast your instance to be able to add it to your list is this:

 myParentClassDatabase.myParentClassList.Add((ParentClass)obj);

However keep in mind that this is completely pointless inside a custom inspector. Unity's serialization system does not support inheritance for custom serializable classes. Make sure you read the page carefully. Fields in MonoBehaviours which have a custom serializable class as type are always serialized based on the variable type and not the actual type. Make sure you read the example in the section

No support for polymorphism

If you need inheritance and you want it to be serializable by Unity you have to derive your base class either from ScriptableObject or MonoBehaviour.

Actually there is a workaround for custom serializable classes, but it requires you to manually serialize / deserialize your custom classes with the aid of the ISerializationCallbackReceiver interface. Though in most cases it's not worth to roll your own serialization system.

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 laoril · Sep 16, 2017 at 08:44 PM 0
Share

Hey Bunny, thank you for your answer. I was assu$$anonymous$$g this was possible since I think I saw people create Item Databes editors to fill their databases of items (weapon, armor, etc...). So knowing what you just told me I assume they have just used a different list for each type, or define their type in an enum or something ;-) I think I'm gonna end up using a type enum and one same class since I'm not going for something too complex, but I still think it's sad we can't do that. Thanks again mate

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

69 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

Related Questions

Dynamic world in Unity 4 Answers

Does Unity support Dynamic cloth? 1 Answer

Setting camera target after I instantiate the gameobject? 2 Answers

Script loads font, how do I change the color? 2 Answers

Bootcamp River prefab with standing water on sides ? 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