- Home /
How do I find the size of a gameObject?
How do I find the size of a gameObject? For example the length of x.
Answer by Vunpac · Jul 01, 2014 at 12:37 PM
Well I came here looking for an objects unit size. I tried Mesh.size, but wasn't the objects unit size. Since it tells you the size of the mesh, not of the scaled mesh. So if you just multiply Mesh.size by transform.localScale, you will get the objects unit size.
Example: a plane in unity is 1, 1, 1 and it's unit (or mesh size) is 10, 0.00002(ish), 10. So if you scale the plans x by 5. It's unit size is 50 even AFTER scaling these are the sizes.
mesh.bounds.size.x = 10
transform.localScale.x = 5
so
mesh.bounds.size.x * transform.localScale.x = 50
This works great, thanks Vunpac!
Vector3 objectSize = Vector3.Scale(transform.localScale, GetComponent().mesh.bounds.size);
I tried this on a plane that was instantiated with a prefab but getComponent<$$anonymous$$esh>()
gave me an exception (no mesh component), that's why I just did: GetComponent<Collider>().bounds.size
. The result was: (10.0, 10.0, 0.3)
(I set the box collider to y=0.25!). Or, even easier if you don't want to use code: Just add a box collider and check it's size in the inspector. ;)
Answer by fireDude67 · Nov 28, 2010 at 06:31 AM
It appears Transform.LossyScale does what you are looking for
lossyScale only tells you the scale of an object, nothing to do with the size.
Oh. I thought the world scale of an object was in units...
Your answer
Follow this Question
Related Questions
Array.length edited using scripts 2 Answers
Check the length of a line renderer? 2 Answers
How to rotate the axis of a 3D GameObject 1 Answer
Destroy the first GameObject out of x fired 1 Answer
C# Variables Transform vs GameObject 1 Answer