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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] How to get other serializedObject's sibling from Editor script?

Discussion in 'Scripting' started by kokizzu, Aug 13, 2019.

  1. kokizzu

    kokizzu

    Joined:
    Mar 7, 2018
    Posts:
    11
    https://answers.unity.com/questions/1657349/

    Continuation from stackoverflow's question. For example I have 2 MonoBehaviour and 1 Editor script:

    Code (CSharp):
    1.     class Bar: MonoBehaviour {
    2.        Button buttonRef2;
    3.     }
    4.     class Foo: MonoBehaviour {
    5.        Button buttonRef1;
    6.        MonoBehavior barRef;
    7.     }
    8.  
    9.     class FooEditor: Editor {
    10.     public override void OnInspectorGUI()
    11.         {
    12.             DrawDefaultInspector();
    13.  
    14.             serializedObject.Update();
    15.  
    16.             Foo foo = (Foo)target;
    17.             if (GUILayout.Button("Bind foo's and bar's Button"))
    18.             {
    19.                 // this set foo's script editor property
    20.                 var buttonRefField = serializedObject.FindProperty("buttonRef1");
    21.                 var button = foo.GetComponentInChildren<Button>();
    22.                 lastButtonField.objectReferenceValue = button;
    23.  
    24.                 // how to get bar's serializedObject from here?
    25.                 // so I can set bar's serializedObject.FindProperty("buttonRef2").objectReferenceValue
    26.  
    27.             }
    28.             serializedObject.ApplyModifiedProperties();
    29.         }
    30.     }

    The question is, if there's gameObject that have both Foo and Bar script, how to get bar's serializedObject from FooEditor script?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Like palex-nx linked.

    Using the constructor you can create new instances of a SerializedObject for a target UnityEngine.Object.

    So just do:
    Code (csharp):
    1. var fooSerObj = new SerializedObject(foo);
     
  4. kokizzu

    kokizzu

    Joined:
    Mar 7, 2018
    Posts:
    11
    damn people, it magically works '__') thank you so much!!!