• 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 Pablomon · Apr 12, 2018 at 09:56 AM · custom-inspectorpropertiesgenerics

Property.FIndproperty( of generic type )

-edited to make it more clear- Hi! I have a class called FloatReference that has a field of type FloatVariable

 public FloatVariable variable;

The FloatVariable class inherits from a Generic class called GenericVariable

 [System.Serializable]
 public class FloatVariable : GenericVariable<float>
 {
 }
 
 [System.Serializable]
 public abstract class GenericVariable<T> : ScriptableObject
 {
       public T value;
 }

   

In my custom property for the FloatReference class I try to get the variable field

 SerializedProperty variable = property.FindPropertyRelative("variable");

But what I get is null.

So I understand FindProperty can't handle generics. Is there a way around t$$anonymous$$s?

Thanks in advance.

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
0

Answer by kskjadav007 · Apr 12, 2018 at 11:41 AM

 public GenericVariable<T> variable;
 
         foreach(variable obj in FindObjectsOfType <Type>())
         {
             NewList.Add (obj);
         }

Not Tested

Comment
Add comment · Show 2 · 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 Bunny83 · Apr 12, 2018 at 12:21 PM 0
Share
avatar image gavriktonio · Jul 24, 2018 at 10:14 PM 0
Share
avatar image
1

Answer by Bunny83 · Apr 12, 2018 at 12:20 PM

Unity's serialization system can't serialize generic types at all (with the only exception of the generic List class). That means your "variable" is not serialized at all. In order to get a serializable version of your class you have to create a non-generic subclass. Just like the generic UnityEvent classes.


Your question is kinda abstract as you did not use any generic type argument in your code snippet. You used "T" here. If your variable declaration is part of another generic class you should add more details about where and how you use your class. What's actually the topmost serialized object (ScriptableObject or MonoBehaviour)?


edit

Since you've added more information here's an update:

You derived your "GenericVariable" from ScriptableObject. ScriptableObjects are "standalone assets". They are serialized as a seperate object (like all classes derived from UnityEngine.Object). That means a field like t$$anonymous$$s:

 public FloatVariable variable;

Is actually a serialized reference. So if you have a SerializedProperty of that "variable" field you can't find any "sub" fields using FindProperty / FindPropertyRelative since the FloatVariable class is serialized on it's own. You would have to create a new SerializedObject for the instance of the FloatVariable in order to get any fields of the FloatVariable.


If that's not what you want to do you have to add more information. You seem to want to find the "variable" field. However finding a relative field only makes sense if you actually inspect the outer class (the class that actually contains the FloatVariable field). You don't be very specific about your case. Do you create a PropertyDrawer or a custom inspector? A bit more context would be great. Maybe you don't want the GenericVariable class to be derived from ScriptableObject?

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 Bunny83 · Apr 12, 2018 at 02:34 PM 0
Share

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

79 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

Related Questions

Generic CustomEditor 0 Answers

MonoBehaviour Properties not saving their values from Editor 2 Answers

Custom Inspectors for generics and tweaking default inspectors 1 Answer

(SOLUTION) How to serialize interfaces, generics, auto-properties and abstract System.Object classes? 3 Answers

Custom Inspector: Iteration/property comparison always false 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