Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Copy/Paste Bug: Parent losing references to fields in Child

Discussion in 'Scripting' started by orionburcham, Aug 28, 2012.

  1. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    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:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Child : MonoBehaviour {
    6.  
    7.     public int num = 5;
    8. }
    9.  
    Parent.cs:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Parent : MonoBehaviour {
    6.  
    7.     public Child child;
    8. }
    9.  
    And a custom Editor for Parents, ParentInspector.cs:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. [CustomEditor(typeof(Parent))]
    7. public class ParentInspector: Editor {
    8.  
    9.     public override void OnInspectorGUI() {
    10.         DrawDefaultInspector();
    11.         EditorGUILayout.PrefixLabel((Parent)target.child.num.ToString());
    12.     }
    13. }
    14.  
    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
     
    Last edited: Aug 28, 2012
  2. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    488
    Bumpy bump bump?

    Could honestly use some help on this one.
     
    Last edited: Aug 28, 2012
  3. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    I have the same problem. I've reverted to always making constructors for child classes, but I'm not sure how that'll work when the child class is also a Behaviour - if I remember correctly, I think Unity complains about overriding Behaviour constructors.