- Home /
How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method?
Hi. I am using this sentence to get the Childrens of the GameObject who has the script. SceneObjects is an array of Transforms.
SceneObjects = this.gameObject.GetComponentsInChildren<Transform>();
The function works well but i get back all the childrens and the father too. Mamely, i get back this "this.gameObject" too and i don't want this. How can i get only the childrens?
Thanks for your help.
If you want just the immediate children, take a look at the bit of script at the top of the Transform reference page. If you need it in array form, you will have to populate the array yourself, but Transform.childCount gives you the size to make the array.
You could also use your original solution and just set the transform of the this.gameObject to null. Your receiving code could then avoid processing a null reference.
Answer by Noctys · Feb 21, 2015 at 11:42 PM
personally I would add using System.Linq;
and then do
SceneObjects = this.gameObject.GetComponentsInChildren<Transform>().Where(go => go.gameObject != this.gameObject);
Using System.Linq gives you access to the Where extension and then you can run a search. You can read more about Linq here: http://www.codeproject.com/Articles/19154/Understanding-LINQ-C, but you are basically running a search on everything returned by GetComponentsInChildren
and only adding it if the search is true.
Although, it would be more efficient to use your code without Linq, and then run your loop like this:
foreach(Transform t in SceneObjects)
{
if(t != this.gameObject.Transform)
{
//... Your code
}
}
The problem with LINQ is that it's not available on all platforms.
Answer by JoshuaMcKenzie · Jan 20, 2016 at 07:06 PM
Create an extension Class
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class UtilityExtensions
{
public static T[] GetComponentsOnlyInChildren<T>(this MonoBehaviour script) where T:class
{
List<T> group = new List<T>();
//collect only if its an interface or a Component
if (typeof(T).IsInterface
|| typeof(T).IsSubclassOf(typeof(Component))
|| typeof(T) == typeof(Component))
{
foreach (Transform child in script.transform)
{
group.AddRange (child.GetComponentsInChildren<T> ());
}
}
return group.ToArray ();
}
}
Then all of your Monobehavior scripts can use it and not only for Transforms, but for any type of component or interface. (yes GetComponent can also get Interfaces!)
SceneObjects = GetComponentsOnlyInChildren<Transform>();
if an invalid type is passed in or if there aren't any of that type in the children, it will return an empty array.
EDIT: I missed the part about only direct children (excluding any further descendants). in that case the code for that isn't that much different from what I already wrote, just swap out the GetComponentsInChildren<T>()
with GetComponents<T>()
You know, it's probably easier just to use GetComponentsInChildren
and then
if ( crt == rt ) continue;
if ( crt.parent != rt ) continue;
to GetComponentsInDirectChildren
Answer by Gab_RiS · Feb 21, 2015 at 11:27 PM
This should do the trick ;)
private Transform[] getFirstChildren(Transform parent){
Transform[] children = parent.GetComponentsInChildren<Transform>();
Transform[] firstChildren = new Transform[parent.childCount];
int index = 0;
foreach (Transform child in children){
if (child.parent == parent){
firstChildren[index] = child;
index++;
}
}
return firstChildren;
}
Answer by Fattie · Jan 20, 2016 at 06:32 PM
Note, a really simple approach is often like this...
public float BiggestRightOffsetInChildren()
{
RectTransform rt = GetComponent<RectTransform>();
float result=0f;
foreach (RectTransform crt in
rt.GetComponentsInChildren<RectTransform>() )
{
if ( crt == rt ) continue;
if ( crt.parent != rt ) continue;
float om = crt.offsetMax.x;
Debug.Log(" tried " +om);
if ( om > result ) result = om;
}
return result;
}
As you can see the first line just skips "itself" and the second line skips all the non-immediate children.
Just include those two lines ...
if ( crt == rt ) continue;
if ( crt.parent != rt ) continue;
... and then do whatever you want with the iteration! Hope it helps
The example there would be used in UI, to find the "most right point" of any of the immediate children.
Answer by Ng0ns · Dec 13, 2019 at 11:44 AM
AFAIK parent is always at the start of the array, so you could just loop through it with a "for" with (i = 1), thus skipping the first item.
for (int i = 1; i < length; i++)
{
...
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
problem moving a prefab object in script (c#) 0 Answers
How to transform gameObjects in array? 1 Answer
Keep adding targets to a list 2 Answers
Rotating a Vector3 in Instantiation 1 Answer