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
1
Question by paifu · Aug 17, 2012 at 02:55 AM · gameobjectreplace

Replace a child gameobject by an other at runtime

I want to replace a child gameobject by an another one at the same level of hierachy.

I tried to copy the transform.parent value of the original object before instanciate a new one and copy the transform on it then destroy the primary child object. But it doesnt work. The instanciate object is not instanciate as a child and i got a MissingReferenceExeption because i try to access a destroyed object.

here is my code :

 using UnityEngine;
 using System.Collections;
 
 public class EditorManager : MonoBehaviour {
 
 void StartPlaceObjs () {
     GameObject mybutton;
     GameObject myref;
 
     myref=GameObject.Find("Button_Objects");
     mybutton=EditorData.editorPrefabs[7];
     
     mybutton.transform.parent = myref.transform.parent;
     
     GameObject.Instantiate(mybutton);    
     GameObject.DestroyImmediate(myref);    
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Aug 17, 2012 at 03:04 AM

You're trying to set the prefab's parent! Get a reference to the newly created object, then set its parent:

... void StartPlaceObjs () { GameObject mybutton; GameObject myref;

 myref=GameObject.Find("Button_Objects");
 mybutton=EditorData.editorPrefabs[7];
 // get a reference to the instantiated object:
 GameObject newObj = GameObject.Instantiate(mybutton) as GameObject;
 // copy parent (maybe you should copy the position and rotation as well)
 newObj.transform.parent = myref.transform.parent;
 GameObject.DestroyImmediate(myref);    

} }

Comment
Add comment · Show 1 · Share
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
avatar image paifu · Aug 17, 2012 at 03:41 AM 0
Share

Thanks a bunch you were right I had to create a reference to a new object first and copy position and rotation as well. I set the name also for future reference. The GameObject.DestroyImmediate had to be replaced by GameObject.Destroy to remove the error message. The script is working perfectly now thanks to you.

 void StartPlaceObjs () {
     GameObject mybutton;
     GameObject myref;
 
     myref=GameObject.Find("Button_Objects");
     
     mybutton=EditorData.editorPrefabs[7];
     
         // get a reference to the instantiated object:
     GameObject newObj = GameObject.Instantiate(mybutton) as GameObject;
         
          // copy parent,pos,rot and set name
     newObj.transform.parent = myref.transform.parent;
     newObj.transform.rotation = myref.transform.rotation;
     newObj.transform.position = myref.transform.position;
     newObj.transform.name="Button_Objects_Down";
 
     
     GameObject.Destroy(myref); 
         
         
   }

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

9 People are following this question.

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

Related Questions

Replace gameobject with another 0 Answers

Replace GameObject with another GameObject 2 Answers

Change the mesh of a gameObject 1 Answer

SOLVED - String replace % with " in C# 1 Answer

Replace GameObject with different FBX 0 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