- Home /
Parent losing references to Child fields after duplication, copy/paste
Running into something strange here. Possibly an editor bug- looking for a solution or workaround:
I've got a very simple scene setup:
"Parent" GO (with Parent script)
"Child" GO (with Child script, with a member int ref = 5), added as a child of "Parent".
And a Custom Editor for Parent that displays the value of it's child's ref int as a prefix label in the inspector.
When I copy/paste "Parent" (therefore copying the rest of it's hierarchy), the copy works correctly. However, when I then copy/paste that copy, The resulting clone won't see Child.ref as being initialized. It will display "0", even though the inspector for the referenced Child shows a correctly initialized "5".
To make things even weirder, if you were to copy/paste that newest (broken) copy, the new resulting setup would work fine. This problem seems to alternate pastes, only breaking every other time.
It seems like the new Child might not be getting initialized correctly. Playing the game or re-importing scripts fixes the problem, so it seems like they're forcing initialization that's not taking place during the normal paste process. any ideas what could be happenining here?
Here are the scripts for each type:
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 ParentInspector.cs:
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(Parent))]
public class ParentInspector: Editor {
//// Members ////
Parent parent;
//// Methods ////
public void OnEnable() {
parent = (Parent)target;
}
public override void OnInspectorGUI() {
EditorGUILayout.PrefixLabel(parent.child.num.ToString());
}
}
Your answer
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
Scriptable Objects, how to force Include in Compile / Build without referencing it in the scene? 1 Answer
Play button does not initialize variables of gameObjects which are parameters of a component? 1 Answer
How to detect if a game object is referenced in an unknown script 2 Answers