- Home /
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.
As written, this is going to attempt to add a "Spot" component to the same GameObject over and over and over..
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.
gameObject.AddComponent doesn't make a new gameObject. That's what you are doing wrong.
Post all of your code on pastebin so we can see what's going on.
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.
I've edited my question with an attempt at your solution, if you could take a look again.
Yeah, you're not making new GameObjects. You're making one gameObject with a "Spot" component added a bunch of times.
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.
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.
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;
}
}
Your answer
Follow this Question
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