Is there any way to walk the heirarchy into a mesh?

say i’m starting from a gameobject. I like to do things by walking around the hierarchy. Right now i’m trying to get an object’s mesh. the same mesh is shared by the mesh renderer, and the mesh collider on the object.

I tried gameObject.collider.sharedMesh, but that throws an error, sharedmesh apparently isn’t a property of a collider. It’s a property of a MeshCollider.

so then i tried gameObject.meshCollider.sharedMesh, but that fails too, because meshCollider isn’t a property of gameObject.

punch wall

the other route doesn’t work either, gameObject.renderer.sharedMesh isn’t a valid property

and of course, neither is gameObject.meshRenderer
or gameObject.meshFilter

is there any way to accomplish this without using getComponent? that feels really unwieldy and annoying to do, and i have this instinctual feeling it’s slow, too. not tested that hypothesis though.

If you are absolutely sure the collider is a mesh collider, you can cast the collider, but a better way would be to use GetComponent().

Javascript:

  var meshCollider = GetComponent(MeshCollider);

C#:

   MeshCollider mc = GetComponent<MeshCollider>();