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 RadWayne · Feb 08, 2014 at 07:09 AM · instantiatechildrenparenting

Instantiate an object as a child of the gameobject's parent

I have an asteroid attached to an empty game object. When the asteroid is destroyed I want to instantiate 2 small asteroids as children of the empty game object.

So from a script attached to the original asteroid I need to instantiate objects as children of the GameObject that the asteroid is a child of via script.

What is the best way to go about this?

Comment
Add comment · Show 2
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 robertbu · Feb 08, 2014 at 07:35 AM 0
Share

Consider an alternate solution. Rather than instantiate the two smaller objects, create all three (one large asteroid and two smaller) as part of the original prefab. Have the two smaller disabled. When the asteroid needs to split, just disable the larger asteroid and disable the the smaller one.

avatar image RadWayne · Feb 08, 2014 at 07:47 AM 0
Share

Something like that might work but I'm already looking at having 500+ asteroids loaded at any one time and I'm afraid of a performance hit by doing things like this.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by supernat · Feb 08, 2014 at 07:24 AM

If you are calling Destroy(GameObject) on the parent asteroid, I believe that won't be destroyed until end of frame, and so if you add 2 child objects at the same time, it may appear to work (parent is still valid) but then destroy parent and children end of frame. If you just want to Instantiate two asteroids that start at the same location and have similar velocity (though you would need to change it so the two objects diverge), you would just call Instantiate(prefab, transform.position, transform.rotation); If you want to parent the two, be sure you don't destroy the parent, you could disable its renderer or something, and call like this:

 GameObject child = Instantiate(prefab, transform.position, transform.rotation);
 child.transform.parent = transform.parent;

Edit: I missed that you wanted to parent the asteroid's parent, so ignore most of this. You can indeed destroy the asteroid if parenting to its parent. I updated the parent line above.

Comment
Add comment · 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
0

Answer by Griffo · Feb 08, 2014 at 07:25 AM

 var asteroid : transform;
 
 private var rock : transform;
 
 function Update(){
 
     if(when you need a asteroid){
         Asteroid();
     }
 }
 
 function Asteroid(){
 
     rock = Instantiate(asteroid, transform.position, Quaternion.identity);
     rock.parent = transform.root;
 }
Comment
Add comment · 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

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

20 People are following this question.

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

Related Questions

How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 Answers

Insantiate object to parent without changing scale? 0 Answers

Are my bounds right world position in this case ? 1 Answer

How to make multiple gameobjects child of one and the same transform (SCRIPT) 2 Answers

Create Clones as children in loops? 1 Answer


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