Hiding the meshes of the children in Prefab

I was following along, but since I personalized the ship with the modules (a Macross Skull-1 inspired, pretty cool)
image

I cannot just find the mesh in the children of the prefab to hide them like the lesson. I did try to use a foreach loop and an array to find and hide the meshes in the children, but only hides the first mesh of the array.
image

I also tried a whole lot of different ways, but I believe this is the one that got me the closest to a solution, so here’s my code if anyone could help:

public class CollisionHandler : MonoBehaviour
{
[SerializeField] float levelReloadDelay = 1.0f;
[SerializeField] ParticleSystem explosion;

[SerializeField] GameObject[] meshChildren;

void OnTriggerEnter(Collider other)
{
    StartCrashingSequence();
}

void StartCrashingSequence()
{
    if (!explosion.isPlaying)
    {
        explosion.Play();
    }
    HideMeshInChildren();
    GetComponent<PlayerControls>().enabled = false;
    Invoke ("ReloadLevel", levelReloadDelay);
}

void ReloadLevel()
{
    int currentScene = SceneManager.GetActiveScene().buildIndex;
    SceneManager.LoadScene(currentScene);
}

void HideMeshInChildren()
{
    foreach (GameObject part in meshChildren)
    {
        MeshRenderer mesh = GetComponentInChildren<MeshRenderer>();
        mesh.enabled = false;
    }
}

}

You loop through the list, but then completely ignore it. So, you find the first mesh on the ship and disable it 16 times.

Use the meshes from the list. Change the above

void HideMeshInChildren()
{
    foreach (GameObject part in meshChildren)
    {
        // HERE: Get the mesh renderer from the part
        MeshRenderer mesh = part.GetComponentInChildren<MeshRenderer>();
        mesh.enabled = false;
    }
}
2 Likes

Holy moly …! :upside_down_face:
I feel stupid… I read through the documentation and totally missed the gameObject in “gameObject.GetComponentInChildren”… :grimacing:

But hey, that’s learning right?!

Thank you so much! You really are a Master Problem Solver!

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

Privacy & Terms