Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
1
Question by Adagio-81 · Nov 11, 2018 at 03:26 PM · editorpropertiesfield

Why can't FindProperty find properties?

I'm trying to update my Editor code to use serializedObject and [CanEditMultipleObjects], but I'm hitting a few problems here. My main problem seems to be the fact that FindProperty can't find properties, but for some reason it can find public fields. This doesn't really make much sense in my world

Here's my code:

 [Serializable]
 public class MushroomPosition: MonoBehaviour
 {
     [SerializeField]
     private int row = 0;
 
     public int Row
     {
         get
         {
             return row;
         }
         set
         {
             if (row == value)
                 return;
 
             row = value;
             DoSomeWork();
         }
     }
 
     private void DoSomeWork()
     {
         // Do some work!!!
     }
 }

 [CustomEditor(typeof(MushroomPosition), true)]
 [CanEditMultipleObjects]
 public class ItemPositionEditor : Editor
 {
     SerializedProperty rowProperty;    
     SerializedProperty rowVariable;
 
     private void OnEnable()
     {
 // There is a property called Row in my MushroomPosition code, but it can't find it. I would assume a method called FindProperty would find the property
         rowProperty = serializedObject.FindProperty("Row");
 
 // There is a public field called row on the object. For some weird reason this actually finds it. I would assume a method called FindProperty would NOT be able to find fields... but it does... ehhhh?
         rowVariable= serializedObject.FindProperty("row");
     }
 
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
 
         EditorGUILayout.IntSlider(rowProperty, -15, 15, new GUIContent("Row"));
 
         serializedObject.ApplyModifiedProperties();
     }
 }
 

Googling for answers I have only been able to find threads where people using FindProperty are searching for public fields on an object, but none where they are actually using FindProperty to... well... you know... find a property..?

I need Editor to use the property to set the value as I need it to execute some code when the value is changed. How can this be done?

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
2
Best Answer

Answer by Hellium · Nov 11, 2018 at 04:28 PM

As indicated by the documentation, FindProperty finds a serialized property. In .NET term, it's a field. Unity deliberately does not serialize getters and setters to prevent possible side effects.


Because row is marked as SerializeField, FindProperty is able to retrieve it.


If you want to use the getter and setter in your custom inspector, I advise you to do as follow:

 public class ItemPositionEditor : Editor
  {
      MushroomPosition mushroomPosition;
      SerializedProperty rowProperty;    
      SerializedProperty rowVariable;
  
      private void OnEnable()
      {
           mushroomPosition = (MushroomPosition ) target;
      }
  
      public override void OnInspectorGUI()
      {
          // Not sure if the following line is needed anymore
          // Unless you need to edit other SerializedProperty 
          serializedObject.Update();
  
          mushroomPosition.Row = EditorGUILayout.IntSlider(mushroomPosition.Row, -15, 15, new GUIContent("Row"));
  
          // Not sure if the following line is needed anymore
          // Unless you need to edit other SerializedProperty 
          serializedObject.ApplyModifiedProperties();
      }
  }


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
avatar image
0

Answer by Lesh48 · May 21 at 02:53 PM

You can simply add [SerializeField] [HideInInspector] to private field and get it from the editor.

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

192 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to make generic options in GameManager that, when changed, will apply to all components of that type? 0 Answers

Change the color of a variable field if it is empty 0 Answers

Cant drag imges from the assets folder 0 Answers

Unity IDE not renderin the user interface 0 Answers

How to set same distance between Canvas buttons with editor? 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