Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by vexe · Jul 29, 2014 at 04:08 PM · serializationmonobehaviourgenerics

Generic MonoBehaviour with nested class serialization issue

This serializes:

 public class GenericMB<T, V> : MonoBehaviour
 {    
     public T  t;  
     public V  v;
 }
   
 public class ThisSerializes : GenericMB<int, string>
 {   
 }

i.e. if I have a GO with ThisSerializes attached to it, 't' and 'v' will show up in the inspector.

This works well too:

 public class KvpMB : MonoBehaviour
 {
     public KVP kvp = new KVP();
 
     [Serializable]
     public class KVP : GenericKeyValue<int, string>
     {
     }
 }

 public class GenericKeyValue< TKey, TValue >
 {
     public TKey key;
     public TValue value;
 }
 
 public class ThisSerializesToo : KvpMB
 {
 }

Howover; not the case here:

 public class GenericMB<T, V> : MonoBehaviour
 {
     public KVP kvp = new KVP();
 
     [Serializable]
     public class KVP : GenericKeyValue<T, V>
     {
     }
 }
 
 public class GenericKeyValue< TKey, TValue >
 {
     public TKey key;
     public TValue value;
 }
 
 public class ThisDoesNotSerialize : GenericMB<int, string>
 {
 }

Attaching ThisDoesNotSerialize to a GO 'kvp' doesn't serialize thus not visible in the inspector.

I don't get why; I'm not attaching a generic MonoBehaviour; It's just that I'm inheriting from one. It serialized in the first case, why doesn't it serialize in this one as well?

Any ideas?

Thanks.

Comment
Add comment · Show 1
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 hexagonius · Jan 19, 2017 at 01:55 PM 0
Share

I even found that having a plain valid nested Serializable class in a generic class does not allow it to be serialized in any derived class.
I couldn't find a restriction of the attribute on $$anonymous$$SDN, so maybe that's a restriction on Unity's side. Since only concrete types can be serialized, maybe such a nested class is not valid since it's containing class is not serializable, too.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jister · Mar 04, 2015 at 08:07 AM

my guess is because your are not serializing the types only the generics so unity doesn't know what to show

      [Serializable]
      public class KVP : GenericKeyValue<T, V>
      {
      }


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

25 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

Related Questions

Start function on a listed non mono script? 2 Answers

Data with reference to scene objects: Monobehaviour or ScriptableObject? 1 Answer

Associate a data object to Monobehaviours with generic 0 Answers

How to correctly use [SerializeReference] on generic types 0 Answers

C# Inheriting from MonoBehaviour but still be able to instantiate with new()? How should I do this? 2 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