How to hide GameObject formed by multiple level modules without Mesh Renderer?

I have created an GameObject that formed by multiple level modules under a GameObject empty called “Player Ship” like below:

Unity - Issues on modules formed GameObject

Later, I discovered there is no Mesh Renderer from the custom formed “Player Ship”,

I found some solution from this THREAD, but it only scan the first level of modules, bypassing 2nd and 3rd level of modules.

The Hierarchy for the “Player Ship”, is like below:

Is confirmed that the 2nd and 3rd level of modules like below is not scanned by the foreach() loop, only all the first level modules were detected, excluding its sub modules, Laser and VFX respectively.

Unity - Hierarchy 2nd 3rd levels not detected

Based on picture highlighted above, how to get the sub renderers respectively.

Please advise.

Hi!

To get all the children of a particular object you need to use the following method GetComponentsInChildren, since getting the component is kinda slow, I highly recommend you cache the information wanted on an array right at the start of the scene.

In the example below I’m searching for a Sprite Renderer component, but you can search for any other type of component.

SpriteRenderer[] spriteRenderers;

private void Awake()
{
    spriteRenderers = GetComponentsInChildren<SpriteRenderer>();
}

If you are planning to use this code on an object that doesn’t have any child, don’t worry, it won’t return any sort of error. If you want to have more flexibility with this, then you’ll need to create a List instead of an array. That’s slightly more complicated:

using System.Linq;

public class ExampleClass : MonoBehaviour
{
    List<SpriteRenderer> spriteRenderers;

    private void Awake()
    {
        spriteRenderers = GetComponentsInChildren<SpriteRenderer>().ToList();
    }
}

Don’t forget to add the namespace System.Linq otherwise you won’t be able to use the ToList method.

Hope this helps!

Hi Yee, thank you for the feedback, I resolved the issues with Transform.GetChild() and MeshRenderer.GetComponent<MeshRenderer>().enabled to recursively loop over instead.

Thats great!

Just keep in mind that doing that has the exact same issues, it’s quite slow, so you are better caching the list or array to use it later.

It also just occurred to me that that there’s a much better way to disable a mesh composed of several game objects, and now I feel kinda silly for not seeing this before because this is such a better way in every regard. Just add an additional parent to all those child objects, then disable the parent, just one line of code, no need for caching, no need for fancy loops, just a single line of code.

" Just add an additional parent to all those child objects, then disable the parent, just one line of code, "

  • I did that but it only disabled the parent not its sub children.

“it’s quite slow, so you are better caching the list or array to use it later.”

  • Sure, meanwhile just testing Unity and merely wanted it to work through difficulties at first.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms