Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

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

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

How to detect if a game object is referenced in an unknown script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges