Unless I’m just completely missing it, is there any simple way to get all the children of an object? IMHO transform.children should return an array containing all children.
What puzzles me is that you already have transform.childCount. I can’t fathom any possible use for transform.childCount without the array to go with it.
You can iterate over the Transform object itself to find its children. See the top of this page for an example of how to do this.
You can also index the transform directly:
a_child = transform[1];
another_child = transform[22];
Edit Apparently I was wrong about this… You can iterate through them using the iterator syntax “for (var xx in transform)”, but can’t index them explicitly.
Alright, I’m kinda necro-posting, but this issue has come up again.
I’m duplicating an object, and I need all the children of the new copy to know where their original counterparts are.
function SetParents(clone : GameObject, orig : GameObject) {
for (t = 0; t < clone.transform.length; t++) { //line 59
o = orig.transform[t];
c = clone.transform[t].gameObject;
c.GetComponent("BotPart").orig =o;
}
}
First of all, can I re re re iterate the request for supporting copying errors out of Unity’s internal console? I can’t fucking FIND the error I’m trying to copy in the system’s console. And retyping them is the sort of repititive work I bought my COMPUTER to do for me…
Second, I get these errors: (typed by hand)
(59) ‘length’ is not a member of ‘UnityEngine.transform’.
(60) Type ‘UnityEngine.Transform’ does not support slicing.
(61) Type ‘UnityEngine.Transform’ does not support slicing.
You can iterate through the children as described above. However you can’t access them by index (ie. using the transform syntax) because the children don’t have a particular order. It may seem so in the hierarchy view, but at runtime they’re just a set (not an array). So that’s why, for better or worse.
d.
How might I accopmlish the goal of each cloned object knowing who its original object is? Do I have to create each individual child by hand?
Not sure what you are trying to do. Are you just cloning the entire hiearchy. If so why not just use Instantiate(Parent) and then set a variabe in the clone to be the parent
theClone : GameObject = Instantiate(theParent);
// Not sure if GetComponent is needed.
theClone.GetComponent(theCloneScript).theParentVar = theParent;
Or to iterate through all the children
for (var child : Transform in theParent.transform)
{
Do what you want here
}
Or if you are just doing something with the BotPart in the children
var theParts = theClone.GetComponents(BotPart);
for (var botPart : BotPart in theParts)
{
botPart.orig = theParent;
}
Or something along those lines.
lfrog: I’m cloning the entire heirarchy (by instantiating the parent for now). The only problem is that each child needs to know its original: the rocket on the left needs to be linked with the rocket on the left in the original; the rear-facing rocket third from the left needs to know where the original’s rear facing rocket third from the left is. And it needs to do this with an arbitrary number of arbitrarily named sub-objects.
I would use tags (or object names) for this?
d.
Ah, I see. One way would be to use SendMessage and have a method in your scripts to update the orig value.
// In the root clone object
function SetParents(clone : GameObject, orig : GameObject) {
SendMessage("SetParent", orig);
}
// Then in each script
function SetParent( parent : GameObject) {
orig = parent;
}
Another way might be to use the name value of game objects and set those fore each game object in the hierarch. Then you would iterate through your clone and then find the corresponding name on the parent. Or it could be done the reverse.
function SetParents(clone : GameObject, orig : GameObject) {
for (var child : Transform in clone.transform)
{
var theParent : GameObject = orig.Find(child.name);
child.GetComponent(BotPart).orig = theParent;
}
}
I have not tested either of the above so they might need a little tweaking 
lfrog: The problem with the first method is that the “orig” value would be the parent object (i.e. each piece would refer to the original bot as a whole).
Renaming each and every object would make very complex code.
I guess the only way I’ll be able to do this is by looping through the children and manually creating/parenting them.
Let me see if I’m right. You want to clone a hierachy of objects, and then make sure that each object in the cloned hierarchy has a reference to its original (and not to the root of the original hierarchy)?
You could do it by making each object in the original already have a component that refers to itself before copying the hierarchy:
// OrigRef.js
var orig : OrigRef;
function Start() {
if(! orig) orig=this;
}
// CloneHierarchy.js
private static function AddOrigRefs(root : GameObject) {
// Will return all transforms in the hierarchy
var all_transforms = root.GetComponentsInChildren(Transform);
for( var t in all_transforms ) {
if(! t.GetComponent(OrigRef) )
t.AddComponent(OrigRef);
}
}
static function CloneHierarchy(root : GameObject) {
// note: we add the OrigRefs to the original
AddOrigRefs(root);
return Instantiate(root);
}
Note that the AddOrigRefs method above uses the fact that all GameObjects have a Transform component attached and that GetComponentsInChildren will traverse the entire hierarchy to get a flattened list of all transforms in the hierarchy to be cloned.
Moved the topic to the Scripting forum, as this is not a missing feature.