Search Unity

OnInspectorGUI() Can I link two GameObjects both ways?

Discussion in 'Editor & General Support' started by HellSinker, May 29, 2016.

  1. HellSinker

    HellSinker

    Joined:
    Apr 18, 2013
    Posts:
    65
    I have two game objects, say ObjA, and Objb.
    ObjA has a list of ObjB objects, which I've made a custom list for, works great :)

    ObjB has a ObjA reference, and I've fiddled for a bit, and think its time to ask for help...

    When I set an ObjB in the custom list of ObjA, I want the ObjA reference in ObjB to update, so that ObjA points to many children objects, but those children objects also have a reference to their parent...

    Here is the snippet of code where I am stuck, I would paste the lot, but seriously, this is not something I just started yesterday....

    Code (CSharp):
    1.         EditorGUILayout.LabelField("Regions in Archetype");
    2.         SerializedProperty regions = serializedObject.FindProperty("Regions");
    3.         for (int i = 0; i < regions.arraySize; i++)
    4.         {
    5.             EditorGUILayout.BeginHorizontal();
    6.             if (EditorGUILayout.PropertyField(regions.GetArrayElementAtIndex(i), GUIContent.none))
    7.                 ((Regions)regions.GetArrayElementAtIndex(i).objectReferenceValue).SetArchetype(((GameObject)serializedObject.targetObject).GetComponent("Archetypes") as Archetypes);
    8.             if (GUILayout.Button(new GUIContent("-", "Delete"), EditorStyles.miniButtonRight, GUILayout.Width(20)))
    9.             {
    10.                 int oldSize = regions.arraySize;
    11.                 regions.DeleteArrayElementAtIndex(i);
    12.                 if (regions.arraySize == oldSize)
    13.                     regions.DeleteArrayElementAtIndex(i);
    14.             }
    15.             EditorGUILayout.EndHorizontal();
    16.         }
    Lines #6 & #7 are not doing much it seems, it certainly isn't going the way I want...
     
  2. crispybeans

    crispybeans

    Joined:
    Apr 13, 2015
    Posts:
    210
    The code seems fine however there is no ObjA and ObjB inditated anywhere. Out of curiosity why don't you use a System.Collections.Generic.List instead ? that should be much easier to manipulate ?