Problems with finding all children that have meshes

I am looking for all objects in a hierarchy that have meshes. This includes the object itself and any of its children. The purpose is to attach a mesh effect.

If I use:

var mesh : MeshFilter = gameObject.GetComponentInChildren(MeshFilter);
mesh.gameObject.AddComponent ("MeshExplode");

It finds the first mesh and works fine. But as I want to have all meshes I am trying the following:

var meshes : MeshFilter[] = gameObject.GetComponentsInChildren(MeshFilter) as MeshFilter[];
			
if(meshes)
{
  for (var mesh : MeshFilter in meshes) 
  {
     Debug.Log("Found mesh =======================================");
     mesh.gameObject.AddComponent ("MeshExplode");
  }
}
else
{
  Debug.Log("No meshes");
}

This doesnt find any meshes and I am completely confused to why.

Is it even getting to the for loop? It’s possible that you can’t cast a MeshFilter to a boolean, and so

if(meshes)

always returns false! Try

if(meshes.Length > 0)

instead, see if that works any better.

Ok, I’ve found my own answer. I put the result of the find straight into the for loop. I have no idea why it didnt work when the result in an array but the following works a treat:

for (var mesh : MeshFilter in gameObject.GetComponentsInChildren(MeshFilter) )
{
Debug.Log("Found mesh =======================================");
mesh.gameObject.AddComponent ("MeshExplode");
}