0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Have you tried calling RecalculateBounds to ensure the bounds are up to date? \$\endgroup\$ – DMGregory Mar 22 at 17:11
  • \$\begingroup\$ @DMGregory THANK YOU! That worked perfectly. Doing that would never have occurred to me. \$\endgroup\$ – JLC Mar 22 at 17:56
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Mar 22 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.