One solution to "GetComponentsInChildren returning parent's component aswell?"

First time posting a thread so I apologize in advance if its not completely correct. I just wanted to post this because it upsets me how usls some of the unity support is and whoever is responsible for not making a solution to this API that would simply have an extra option to “ExcludeParent”.

This simple solution worked for any component that I wanted.

Credit to @Vadimskyi on this post GetComponentsInChildren - not parent and children is where he posted this solution but its a bit buried and hidden in the thread

private MonoBehaviour[ ] childrenScripts;

void Awake()
{
childrenScripts = GetComponentsInChildren();
}

void Update()
{
foreach (var script in childrenScripts)
{
//Ignore the parent’s monobehavior
if (script == this.GetComponent())
continue;

script.enabled = true;
}
}

It likely upsets some of the Unity staff who are being called useless too. Because you don’t like how a method works is no reason to be rude so please, discuss but be respectful to all.

The docs state quite clearly its function and this subject has been discussed to death on these forums, even recently here .

Side-note: How to post code on the forums.

Thanks.

Ok I will apologize for my rudeness it was unnecessary and uncalled for but the criticism should still stand as this has been a problem for years now like you said it has been discussed to death. This also has happened an incredible amount times. Once again I apologize but I do think I am being fair with the criticism as in 5 years of me programming most of the solutions on these forums that are more clearly stated and better in implementation come from the community and almost never from the support.

That solution is also pretty terrible and I don’t think actually works as you think it does. Using this.GetComponent<Monobehaviour> will return the first monobehaviour component on the same game object as the calling component (their order matters). Which means if there’s another monobehaviour before this component on a game object, you’ll be comparing against that and skipping past it, leaving you to possible still be iterating on the same instance as this.

If your intent is to skip the same component invoking the call, then you want to compare against this. Ergo:

foreach (var component in childrenComponents)
{
    if (component == this)
    {
        continue;
    }
}

(fyi they’re components, not scripts).

Or if you intend to skip all components on the same game object that was the root call for GetComponentsInChildren<T> then you want to compare each component’s game object, like so:

foreach (var component in childrenComponents)
{
    if (component.gameObject == this.gameObject)
    {
        continue;
    }
}

So if we’re trying to provide solutions let’s first make sure they’re actually working ones.

Fair enough, but this will also be considered a different problem right? Finding all scripts or the one that you specifically want in the children or parent. This code implementation mostly to show how to skip the parent for the GetComponentsInChildren API. In case I am once again wrong do please correct me for future devs to have this solution completely solved.

That’s the thing. Your code doesn’t. It only skips the first monobehaviour component on the same game object, which is very unlikely to be the behaviour anyone wants. If there’s multiple monobehaviours on said game object then you won’t skip any beyond the first.

My solutions skips the same component in the first example, and the parent game object in the second example (the problem you were trying to solve).

Unity support isn’t there to teach you how to code.

And yet, point was proven once again, its like a firefighter telling you where the bucket and water are so you can go and put the fire out yourself, or ask your neighbors to help. Thanks for the solution though.

What “fire” though? :slight_smile: