- Home /
How to get all children gameobjects
Ok I have seen this asked a few times but I have not seen a satisfactory answer. I have a space ship that is made up of a bunch of game objects that are all parented to a "command seat" game object. These objects all have a script that holds variables like thrust and power consumption. I want the command chair to have a function that iterates through all of its children and adds up all of the variables to be used to calculate the ships total thrust and such. The reason I am doing it this way instead of simply setting these values on the command chair is that the parts that make up the ship can be destroyed. When this happens the function that tallied up all the variables would be run again to get the new variables. This way if you blow up the ships engines it actually gets slower.
Problem is, unity seems to have a hard time actually getting children game objects. I have tried this code:
function CompileShip()
{
for(var child in transform)
{
//child is your child transform
Debug.Log(child);
var moduleThrust = child.GetComponent(ModuleScript).Thrust;
}
}
it does return the child transforms, but I have no idea how to access the gameobject, or the components from that. The last line "var moduleThrust = child.GetComponent(ModuleScript).Thrust;" does not work, it was more of an example of what I would like to do.
So again, the core issue here is getting information out of the script component of the children. But I was only aware of the "getComponent()" method. If there is some way to get the gameobject out of the transform, or if there is a way to get the script components please let me know.
Thanks in advance to anyone who can help.
This doesn't even make sense. Why can't I get the children of an object directly. Why do I have to do this backwards get transform then try to get a game object from it. Isn't a transform the location, rotation and scale of an object? I mean the way this is set up it is almost as if the programmers never expected anyone to possibly need to access anything else [insert dripping sarcasm here].
Answer by CiberX15 · Aug 10, 2013 at 11:42 PM
Ok well this works:
function CompileShip()
{
var allChildren = gameObject.GetComponentsInChildren(Transform);
for (var child : Transform in allChildren)
{
// do whatever with child transform here
var Module : GameObject = child.gameObject;
var childStats : ModuleScript;
childStats = Module.GetComponent(ModuleScript);
if(childStats != null)
{
Debug.Log(Module);
//TODO add up all the variables from the children
}
}
}
I still think it is a rather round about way of getting what I want, and it generates an implicit downcast warning but it works. If anyone figures out a better way of doing this please post it, otherwise I guess this is how it's done.
A specific reason you are first getting the transforms and then your specific component, ins$$anonymous$$d of getting your specific component directly?
GetComponetsInChildren returns Component not Transform. Its rather backwards I agree.
You could use pragma downcast http://forum.unity3d.com/threads/63114-What-is-pragma-implicit-and-pragma-downcast to get rid of the warning at least.
I did find a solution here on answers once that was better. Ill post if I find it again
It's my impression that using for (var child in transform)
is much faster.
So true. But still, on my brief test, I only had the first level...
Answer by Jamora · Aug 10, 2013 at 06:05 PM
You can access the GameObject a transform belongs to with .gameObject. To access the gameobjects of all your child transforms, you'd need
var moduleThrust = child.gameObject.GetComponent(ModuleScript).Thrust;
EDIT: If you use inheritance, or happen to have the same script you need to get from each child, you could also use GetComponentsInChildren to find all the thrust-adding types and then just iterate over the array to get the total thrust. Something along the lines of Thrust[] modules = gameObject.GetComponentsInChildren();
from your command seat gameobject.
I tried that, it returns an error : 'gameObject' is not a member of 'object'. (BCE0019)
Answer by whydoidoit · Aug 10, 2013 at 10:30 PM
So presuming you have a Thrust script attached to every game object that provides Thrust (and might therefore be destroyed) and this has a Value (for the thrust) you can sum them like this:
import System.Linq;
...
var totalThrust = GetComponentsInChildren.<Thrust>().Aggregate(0f, function(current, thrust) current + thrust.Value);
Answer by Linus · Aug 11, 2013 at 12:45 AM
for (t in transform.GetComponentsInChildren.<Transform>()) {
Debug.Log('child name'+t.name);
}
As mentioned I had seen a topic on this here at answers. But I could not find it again. It probably suggested this. It does not give any error and works with pragma strict.
I would use it like this:
#pragma strict
import System.Collections.Generic;
//Find all modules, and store them in a generic list
// See http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use? for information on usage of Generic List
var shipModules : List.<ModuleScript> = new List.<ModuleScript>();
function Start () {
for (m in transform.GetComponentsInChildren.<ModuleScript>()) {
shipModules.Add(m);
}
}
function GetTotalThrust(){
var totalThrust : float;
for(m in shipModules){
totalThrust += m.Thrust;
}
return totalThrust;
}
Answer by g4tobauer · Apr 28, 2015 at 03:19 AM
Component[] cps = gameObject.GetComponentsInChildren<Component>();
foreach (Component cp in cps)
{
print(cp.gameObject.name);
}
use this...
Your answer
Follow this Question
Related Questions
Accessing children of instances vs children of original prefab 1 Answer
How Should I Get a List of Child Objects 2 Answers
Getting transform info after branch 1 Answer
modifing enemies target inside a radius 1 Answer
Is there any way to use parenting as an organization tool without creating a Transform bond? 3 Answers