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 RequiemOfTheSun · Feb 10, 2013 at 07:36 PM · instantiatehierarchychildren

Is there a way to make clones appear as children in the hierarchy?

I have a number of cloned objects from an Instantiate() and at runtime they all appear in the root of the hierarchy. Is there a quick way to get them to appear as children instead?

This is simply for aesthetics and ease of debugging.

edit: Thanks insominix and flaviusxvii, you both gave the correct answer. I was just having trouble seeing it because of my poorly thought out Spot class. I now have refactored the class.

Comment
Add comment · Show 4
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 flaviusxvii · Feb 12, 2013 at 12:07 AM 0
Share

As written, this is going to attempt to add a "Spot" component to the same GameObject over and over and over..

avatar image insominx · Feb 12, 2013 at 12:12 AM 0
Share

Try a much more simple case without arrays or the triple for loop. $$anonymous$$aybe just instantiate an object in a script and try changing its parent. If that works, maybe it'll give you a clue.

avatar image flaviusxvii · Feb 12, 2013 at 01:49 AM 0
Share

gameObject.AddComponent doesn't make a new gameObject. That's what you are doing wrong.

avatar image flaviusxvii · Feb 12, 2013 at 01:59 AM 0
Share

Post all of your code on pastebin so we can see what's going on.

3 Replies

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

Answer by flaviusxvii · Feb 10, 2013 at 08:55 PM

I use a few global gameobjects with zeroed out transforms for the purpose of keeping the hierarchy tidy.

 // This is in my weapon script.. it makes the container object for all ordinance.
 _ordinanceParent = GameObject.Find("OrdinanceParent");
 if(!_ordinanceParent) {
     _ordinanceParent = new GameObject("OrdinanceParent");
 }

 // Later on when the weapon fires I put the new ordinance gameobject in the container..
 GameObject s = Instantiate(ordinance, emitWorldSpace, transform.rotation) as GameObject;
 s.transform.parent = _ordinanceParent.transform;

Hope this helps.

Comment
Add comment · Show 3 · 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 RequiemOfTheSun · Feb 12, 2013 at 12:08 AM 0
Share

I've edited my question with an attempt at your solution, if you could take a look again.

avatar image flaviusxvii · Feb 12, 2013 at 12:13 AM 0
Share

Yeah, you're not making new GameObjects. You're making one gameObject with a "Spot" component added a bunch of times.

avatar image RequiemOfTheSun · Feb 12, 2013 at 02:02 AM 0
Share

Sorry for not perceiving what you meant earlier. You've nailed my problem. I was muddled with what was really going on in my code versus what I thought was.

avatar image
0

Answer by RolandasR · Feb 10, 2013 at 08:12 PM

Don't think so. The best you can do is to set transform.parent but it will create transformation headaches. You can create some empty "clones container" object as root and assign it as parent of clones to clean up.

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 insominx · Feb 10, 2013 at 08:46 PM

Yes. All you need to do is set their parent to the Transform you want them to be under. So something like this:

 Transform instance = (Transform)GameObject.Instantiate(objectToClone);
 instance.parent = newParentObject.transform;
         

You just need a way to get which object you want to parent it to. You could either get it from a property in your class script or even find it by tag (useful for prefabs).

*Edit I just tried out a simple example with no problem:

 using UnityEngine;
 using System.Collections;
 
 public class TestReparent : MonoBehaviour {
     
     public Transform newParent;
 
     void Start () {
         
         GameObject newObject = new GameObject();
         newObject.transform.parent = newParent;
     
     }
 }
 
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

12 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

Related Questions

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

Checking if object intersects? 1 Answer

Accessing children of instances vs children of original prefab 1 Answer

transform.Find always returns null 1 Answer

Enemies instantiate, in the wrong place, and don't show up in the Hierarchy 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