Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Setting property.FindPropertyRelative values?

Discussion in 'Scripting' started by Freaking-Pingo, Jul 10, 2014.

  1. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Hi there Uniteers, I am trying to create a properDrawer for this class:

    Code (CSharp):
    1.  
    2. [System.Serializable]
    3. public class SceneObject : System.Object {
    4.  
    5.     [SerializeField]
    6.     public string sceneObjectID = "";
    7. }
    However, I can't seem to change the value of sceneObjectID through a PropertyDrawer. The code which I used for testing is:

    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(SceneObject))]
    2. public class SceneObjectDrawer : PropertyDrawer
    3. {
    4.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    5.     {
    6.         SerializedProperty sp = property.FindPropertyRelative("sceneObjectID");
    7.  
    8.         Debug.Log("Before: " + sp.stringValue); // Is always empty
    9.         sp.stringValue = "Test"
    10.         Debug.Log("After: " + sp.stringValue); // Is always "Test"
    11.     }
    12. }
    When debugging sp.stringValue, it comes out empty in the "Before" Debug.Log, and in the "After" the value have actually changed. This happens each time the code is executed. What is wrong here, why doesn't sceneObjectID keep being set to "Test"?
     
    Last edited: Jul 10, 2014
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    The method "FindPropertyRelative" doesn't appear to be documented; although I do see it in example listings... probably a documentation error.

    In my custom property drawers I use the following method which seems to work fine for me:
    http://docs.unity3d.com/ScriptReference/SerializedObject.FindProperty.html
    Code (csharp):
    1.  
    2.     var sp = property.FindProperty("sceneObjectID");
    3.  
    Is your custom property drawer being shown within a custom inspector? If so then you should ensure that you are using the following:
    Code (csharp):
    1.  
    2. public class YourInspector : Editor {
    3.     public override void OnInspectorGUI() {
    4.         // Update serialized properties from objects current state.
    5.         serializedObject.Update();
    6.  
    7.         // Present property drawer for some property.
    8.         EditorGUILayout.PropertyField(theProperty);
    9.  
    10.         // Apply changes to object.
    11.         serializedObject.ApplyModifiedProperties();
    12.     }
    13. }
    14.  
     
    kmedved likes this.
  3. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    As far as I understand, FindProperty is for SerializedObjects, but I am working with a PropertyDrawer, and so the input parameter of OnGUI is a SerializedProperty not a SerializedObject. The script I am working on is not an inspector window but an editor window.
     
  4. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Oh yeah, I got it working. It appears I didn't make use of "serializedObject.ApplyModifiedProperties();" Adding that line to the code solved my problem. Thanks numbercruncher :)
     
    kmedved and Thorny2000 like this.