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
4
Question by vexe · Feb 01, 2014 at 02:02 PM · serializedpropertyserializedobjecteditorscript

FindRelativeProperty never worked for me? how does it even work?

Using serialized properties/objects is very beneficial - but I always ran away from them in editor code cause I was never able to get a non-null/legit value out of FindRelativeProperty #-___-

Here's what I have:

FSMTrigger.cs

   public class FSMTrigger : BaseTrigger, Interaction
   {
     public List<FSMTriggerState> states = new List<FSMTriggerState>();
         // stuff...
   }

FSMTriggerState.cs

 public class FSMTriggerState : MonoBehaviour
 {
     public List<int> ints = new List<int>() { 1, 2, 3, 4, 5 }; // for testing
     public List<FSMTriggerTransition> transitions = new List<FSMTriggerTransition>();
     public FSMStateType stateType = FSMStateType.Other;
 }

FSMTriggerTransition.cs

   [ExecuteInEditMode]
   public class FSMTriggerTransition : MonoBehaviour
   {
     [HideInInspector] public List<EventDelegate> onTransition = new List<EventDelegate>();
     public FSMTriggerState toState;
     // stuff...
   }

For the sake of completeness:

 public abstract class BaseTrigger : MonoBehaviour
 {
    // stuff...
 }

 public interface Interaction
 {
     void Interact(UnityEngine.GameObject actor);
 }

Here's what I'm doing:

FSMTriggerEditor.cs

 void Test()
 {
         var spStates = serializedObject.FindProperty("states");
         for (int i = 0; i < spStates.arraySize; i++) {
             Debug.Log("STATE TYPE: " + spStates.GetAt(i).GetValue<FSMTriggerState>().name);  // <--- THIS WORKS
         }
 
         var ints = spStates.FindPropertyRelative("ints");
         for (int i = 0; i < ints.arraySize; i++) {
             Debug.Log("Numbers: " + ints.GetAt(i).GetValue<int>()); // THIS DOESN'T
         }
 
         var spTransitions = spStates.FindPropertyRelative("transitions");
         for (int i = 0; i < spTransitions.arraySize; i++) {
             Debug.Log("Transitions: " + ints.GetAt(i).GetValue<FSMTriggerTransition>().name); // NOR THIS
         }
 
 }

I wanted to make sure that the problem is NOT with the state class itself, so I went ahead and:

FSMTriggerStateEditor.cs

 private void test()
 {
     var spTransitions = serializedObject.FindProperty("transitions");
     for (int i = 0; i < spTransitions.arraySize; i++) {
         Debug.Log("Transitions names: " + spTransitions.GetAt(i).GetValue<FSMTriggerTransition>().name); // THIS WORKED! SO AS THE INTS OF COURSE
     }
 }

So it's pretty obvious that FindRelativeProperty isn't working right - maybe I just don't know how to use it - but I've seen a lot of scripts just reference the thing like I did - there's no special path - just the name of the variable I want to fetch. Yet it's not working with me - I get null reference exceptions. Even if I try the simplest of examples :(

Any help would be greatly appreciated - thank you very much in advance.

EDIT: There was a mistake in the test script using FindRelativeProperty - although I fixed that, it's still giving me a null ref:

     var spStates = serializedObject.FindProperty("states");
     for (int i = 0; i < spStates.arraySize; i++) {
         var state = spStates.GetAt(i);
         Debug.Log("STATE TYPE: " + state.GetValue<FSMTriggerState>().name); // WORKS

         var ints = state.FindPropertyRelative("ints");
         for (int j = 0; j < ints.arraySize; j++) { // BREAKS HERE - MEANING INTS IS NULL
             Debug.Log("Numbers: " + ints.GetAt(j).GetValue<int>());
         }
     }


EDIT:

Forums crosslink.

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

Answer by Roland1234 · Feb 02, 2014 at 07:41 AM

Hey vexe!

I always run into issues with Unity's serialization when working on editor code. In my experience the SerializedProperty.FindPropertyRelative method is not meant to be used when the SerializedProperty is referring to a MonoBehaviour-derived object (like your FSMTriggerState object is), I've only had success using it when the object being referred to is a regular class decorated with the [Serializable] attribute.

For SerializedProperties that reference MonoBehaviours you could new up an instance of a SerializedObject passing in SerializedProperty.objectReferenceValue (if it isn't null) to the constructor, then do a regular FindProperty off of that. But in your case you're actually dealing with a List of MonoBehaviours, so you'd have to do that for each element (or else pass in the entire array, but I think that'll have unintended consequences) and may want to consider a different approach altogether. It's possible that your FSMTriggerState class shouldn't be a MonoBehaviour at all - and if it should, then you'll probably have to accomplish what you want in its own editor instead of trying to access it from another.

I hope that helps - Unity editor details really can be a pain sometimes, and the documentation really isn't as clear as it should be. Good luck though!

Comment
Add comment · Show 5 · 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 vexe · Feb 02, 2014 at 01:15 PM 0
Share

Thanks a lot for responding and answering me - Your approach seems to be the way to go - I haven't thought of that actually, I'll give it a go and let you know.

avatar image vexe · Feb 02, 2014 at 01:21 PM 0
Share

Holy! - IT WOR$$anonymous$$ED! newing up a SerializedObject is the solution! - THAN$$anonymous$$ YOU! +1000000000000000000000000

avatar image fusecore · May 17, 2016 at 02:08 PM 1
Share

Wow thank you, making a new SerializedObject fixed my issue with ScriptableObjects too. http://answers.unity3d.com/questions/1188088/inspector-cant-find-field-of-scriptableobject.html#answer-1188103

avatar image Hanh fusecore · May 24, 2018 at 09:49 AM 0
Share

thank @fusecore

avatar image Bubsavvy · Oct 12, 2019 at 08:27 PM 0
Share

$$anonymous$$aking a new SerializedObject is not the solution for my particular situation. The scriptable objects im working with have nested properties that are expandable. Creating a new instance detaches the connection of those properties from the original serializedobject meaning the expandable properties isExpand will always return false ins$$anonymous$$d of maintaining their state. Its really frustrating actually. I do feel like this is a bug. The docs have a terrible explaination of FindPropertyRelative's intention.

avatar image
0

Answer by winxalex · Apr 06, 2016 at 07:48 AM

Same as @vexe I've list of serializedProperties in reorderable list. So If I want subproperty of those property I need private void DrawConditionsElement (Rect rect, int index, bool selected, bool focused) {

             SerializedProperty eventConditionSerializedProperty = this.m_ConditionList.serializedProperty.GetArrayElementAtIndex (index);
 
 SerializedProperty eventFieldSP=new SerializedObject(eventConditionSerializedProperty.objectReferenceValue).FindProperty("eventField");
 }
 
 public class EventCondition:ScriptableObject{
   public UnityEvent eventField;
 }

So not just in MonoBehavours but also in ScriptableObject FindPropertyRelative is useless. What bother me is that now I need to keep array of eventFieldSP, keep track of add remove, not to do newing every draw.

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

23 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

Related Questions

Changing Inspector's Serialized Property Label (With Code Sample) 1 Answer

Change the fields on a object bound to a SerializedProperty via custom inspector? 1 Answer

Calling a method on a serialized property (Custom Editor with Reorderable List) 0 Answers

Serialized Property being uneditable in certain cases 0 Answers

Incrementing a SerializedProperty on a per-object basis 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