- Home /
Find Size of GameObject
Hi there,
I would like to know how to find the size of an object. eg vector3(10.0f,10.0f,10.0f); gameOjbject.size:vector3
What code would I have to use to find the total size of a GameObject? How would I do it.
Thanks
Answer by Mike 3 · Aug 03, 2010 at 11:12 AM
If the object has a renderer, then renderer.bounds.size will give you what you want
Next best thing would probably be collider.bounds.size
Thank you for this post. When I try this with the collider.bounds.size, it always returns (0,0,0). However, the render.bounds.size works like a charm for objects with a renderer attached. Any idea what would cause the collider.bounds.size call to return vector3.zero? I ask because I have some triggers in the game world that I want to query the size of to avoid hard coding the sizes. Thanks.
Never $$anonymous$$d, I think I found the answer. I think that it is because the object I was testing with is a prefab that I instantiate and is not yet placed in the scene, so it is inactive.
Thanks... of course a forum is meant for people who get stuck using the basic documentation... But it's working now.
Note that this give the axis-aligned, world space bounding box (as per the mentioned Renderer documentation), so if your object is rotated then renderer.bounds and collider.bounds will change.
Answer by obiolg · Mar 19, 2015 at 12:16 PM
The correct code for recent version of Unity is:
GetComponent<Collider>().bounds.size
GetComponent<Renderer>().bounds.size
I seem to not have issues with renderer when there is a renderer, but collider always seems to return vector3.zero. Any ideas?
Quick thought based on jdweiber's comment: is your object instantiated in the scene?
I had the same issue, but it was because I'm working with a Collider2D. There was no Collider component, so it returned zero.
Answer by DCorboy · Mar 28, 2015 at 01:11 AM
I believe the correct Unity5 code is:
GetComponent.<Collider>().bounds.size
GetComponent.<Renderer>().bounds.size
The '.' before the requested component is needed.
This is incorrect, obiolg's answer shows the correct code.
This must be UnityScript's syntax. In any case make sure the collider is enabled and the object active.
Yes, that's UnityScript. Based on ActionScript, which also features that atrocity
Answer by Darkgaze · Nov 07, 2018 at 10:16 AM
transform.bounds gives you the global size, axis oriented.
if you want the REAL size of the object, ignoring the transformations done to the object, use mesh.bounds which gives you the local size of the mesh.
Can you provide a link to the docs for Transform.bounds
? I can't find it in the script reference. I think you might be mistaken.
Answer by mheyman · Jan 12, 2017 at 10:21 AM
For 2D sprite:
var p1 = gameObject.transform.TransformPoint(0, 0, 0);
var p2 = gameObject.transform.TransformPoint(1, 1, 0);
var w = p2.x - p1.x;
var h = p2.y - p1.y;
This works whether or not the item is active.
This did not work for me. For my 2D sprite GameObject your code gave me a w=-19 & h=1. I know my GameObject is 6840 unity units wide and 504 units tall. The sprite itself is 360x504 pixels with Pixels per Unit = 1. And the Transform Component of the GameObject has X Scale of 19 and Y rotation of 180. I see that your w is negative because you're not accounting for rotation, but it doesn't look like you're accounting for the size of the sprite at all, either.
Worked perfectly for me. w corresponded to the game object size I got in the inspector. I was interested in the screen size (sw) which I converted as below. It changes according to the size of the viewport, as expected
var p1 = ballControl.transform.TransformPoint(0, 0, 0);
var p2 = ballControl.transform.TransformPoint(1, 1, 0);
var s1 = refPosition = Camera.main.WorldToScreenPoint(p1);
var s2 = refPosition = Camera.main.WorldToScreenPoint(p2);
var w = p2.x - p1.x;
var sw = s2.x - s1.x;
Your answer
