# Can't refer mesh size after using .BakeMesh() in Unity

I can't figure out how to refer to the size of a particular mesh.

The mesh is created through .BakeMesh(), then a box collider is added. Unfortunately the box collider has a scale of 1 by 1 by 1, regardless of how big the mesh I'm adding it to seems to be.

This doesn't match my understanding of how creating a box collider should work, so I've set up a Debug.Log that outputs the mesh size.

Here the plot thickens. The mesh's size just comes back at 0, 0, 0. My attempts at getting the size of the object are as follows:

objectToMeasure.GetComponent<Renderer>().bounds.size;

objectToMeasure.GetComponent<MeshFilter>().mesh.bounds.size

objectToMeasure.GetComponent<MeshFilter>().sharedMesh.bounds.size;


Assuming I'm referring to the size of the object in the correct way, this seems like it must be an issue created by baking the object. Do I need to wait a frame (via starting a coroutine) before trying to access the size of the object, or am I referring to the wrong component of the object, or is the BoxCollider setting it's size based on objectToMeasure's scale rather than it's size in the world?

I feel sure that objectToMeasure.GetComponent().mesh.bounds.size; should be right, since the script I use to bake to the mesh is: meshToBake.BakeMesh(meshToMeasure.GetComponent().sharedMesh);

I've been driving myself to the edge of sanity for 24 hours with this script not working, so advice is greatly appreciated.

• Have you tried calling RecalculateBounds to ensure the bounds are up to date? – DMGregory Mar 22 '19 at 17:11
• @DMGregory THANK YOU! That worked perfectly. Doing that would never have occurred to me. – JLC Mar 22 '19 at 17:56
• If that worked for you, feel free to write it up as an answer. :) In future, remember to check through the docs for the types you're using to see if they have a method like this — it's a good way to spot possibilities that might not have crossed your mind. – DMGregory Mar 22 '19 at 18:11