• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by orionburcham · Aug 27, 2012 at 07:04 PM · editorreferenceduplicateinitializationpaste

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());
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Duplicated Terrain Object and Data STILL affects other terrains 4 Answers

Changing appearance on Awake, how do you preview this in the editor? 2 Answers

Scriptable Objects, how to force Include in Compile / Build without referencing it in the scene? 1 Answer

Array values not being initialised unless the object with the (Player) script is selected in the inspector whilst the game is on 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges