Running into a weird issue when trying to copy and paste nested hierarchies. If anyone could take a look (or knows what’s going on) it would be a huge help- and thank you.
Ok, here’s the setup- Three scripts:
Child.cs:
using UnityEngine;
using System.Collections;
public class Child : MonoBehaviour {
public int num = 5;
}
Parent.cs:
using UnityEngine;
using System.Collections;
public class Parent : MonoBehaviour {
public Child child;
}
And a custom Editor for Parents, ParentInspector.cs:
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(Parent))]
public class ParentInspector: Editor {
public override void OnInspectorGUI() {
DrawDefaultInspector();
EditorGUILayout.PrefixLabel((Parent)target.child.num.ToString());
}
}
Create “Parent” and “Child” GameObjects, and add their corresponding scripts. Add “Child” under “Parent” in the hierarchy:
Add “Child” as the Child field of “Parent” in the inspector. Look at the last line in the Parent inspector- that “5” is now referencing “num” in its “Child”'s script:
Ok! Here’s where we reproduce the bug:
Copy “Parent” once. Check the “5” label in the Parent’s inspector. Should be ok. Now, copy that copy you just made. Check out the inspector for this one. That “5” is now a “0”. <—What is?? If I wasn’t referencing a value type like an int here, it would be giving you a null reference error instead. For some reason, this Parent doesn’t see any of the fields in it’s Child as being initialized:
(When this copy of a copy is selected…)
(…It doesn’t see it’s child.num as initialized: )
one note: hitting play or recompiling scripts fixes the problem. I’m guessing something isn’t getting properly initialized in the Editor during a regular copy/paste operation. Does anyone know a way to manually force initialization of scripts on an object?
Many thanks for any help,
Orion