- Home /
Getting positions of all children in a different gameObject
I've be searching for a way to get access to children of a separate object, so that I can have their positions, but the only method I could find regarding getting multiple children from one transform was: // Moves all transform children 10 units upwards!
for (var child : Transform in transform) {
child.position += Vector3.up * 10.0;
}
So is there a way to rephrase this to get transforms from other objects? I am looking to do something like this:
for (var child : Transform in transform) {
objectPositions[i] = child[i].position;
}
Answer by Danmietz · Sep 13, 2013 at 03:22 AM
GetComponentsInChildren would probably help.
I'm not certain of the JavaScript syntax, but it'd probably be something along the lines of
var otherTransforms : Transform[];
otherTransforms = otherObject.GetComponentsInChildren(Transform);
for (var trans : Transform in otherTransforms)
{
// Do things with each transform
}
would probably do the trick
I don't see a difference between your code, and:
for (var child : Transform in transform) {
// do something
}
Sorry, I updated the example.
If you call GetComponentsInChildren on an object with a given type, it will recursively find all of the components in all of the children of that object and return it. with this, you now have called it on a separate object, as well as storing it for use elsewhere (in otherTransforms).
Ahhh wire! :D - now you got it right. Just tested it out, works fine. @ForgeStudios here's your answer. And if you're having trouble assigning stuff in for loops, go with this:
var otherTransforms : Transform[];
otherTransforms = otherObject.GetComponentsInChildren(Transform);
for (var i = 0, len = otherTransforms.Length; i < len; i++) {
var child = otherTransforms[i];
objectPositions[i] = child.position;
}
@vexe trying to return the children to the Transform array returns this: InvalidCastException: Cannot cast from source type to destination type. SaveLoadData.collectData () (at Assets/UnitySerializer/Scripts/SaveLoadData.js:113) Pause$$anonymous$$.EscapeWin (Int32 windowID) (at Assets/UnitySerializer/Scripts/Pause$$anonymous$$.js:157) UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/GUI.cs:1324)
The problem was the line:
var otherTransforms = terrain.GetComponentsInChildren(Transform) as Transform[];
After this, otherTransforms
is null - You can easily avoid that problem using the generic version GetComponentsInChildren < Transform > ()
- In my C# code I tested out the generic, no problem, I also tested out this, no problem as well:
var otherTransforms = GetComponentsInChildren(typeof(Transform));
$$anonymous$$ess around with GetComp till you get it right, I advise you to debug and see the return values your self, if you're on $$anonymous$$onodev, or VS.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114050im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotate transform without rotation the children 1 Answer
Transform [] = all children of this game object ? (Spawnpoints) 3 Answers
Does a child transform take the name of its parent in unity 3D? 1 Answer
How to get all children gameobjects 5 Answers
How to move the parent object to a child of one of its children? 2 Answers