Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
26
Question by Kruga · Aug 03, 2010 at 10:46 AM · gameobjectsizedimensions

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
50
Best Answer

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

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jdwieber · May 31, 2012 at 06:00 PM 0
Share

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.

avatar image jdwieber · May 31, 2012 at 06:32 PM 0
Share

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.

avatar image jdwieber · May 31, 2012 at 07:47 PM 0
Share

Neither method works for an LOD group :(

avatar image KokodokoGames · Aug 31, 2015 at 01:03 PM 1
Share

Thanks... of course a forum is meant for people who get stuck using the basic documentation... But it's working now.

avatar image FurretTurret · Jan 16, 2017 at 08:38 PM 1
Share

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.

Show more comments
avatar image
38

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
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shaneparsons · Jul 19, 2016 at 08:41 PM 0
Share

I seem to not have issues with renderer when there is a renderer, but collider always seems to return vector3.zero. Any ideas?

avatar image DCorboy shaneparsons · Jul 19, 2016 at 11:47 PM 0
Share

Quick thought based on jdweiber's comment: is your object instantiated in the scene?

avatar image shaneparsons DCorboy · Jul 20, 2016 at 02:23 PM 0
Share

It is indeed

Show more comments
avatar image Kelgand shaneparsons · Apr 26, 2017 at 01:48 AM 0
Share

I had the same issue, but it was because I'm working with a Collider2D. There was no Collider component, so it returned zero.

avatar image
4

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adder_be · Apr 13, 2015 at 12:03 PM 1
Share

This is incorrect, obiolg's answer shows the correct code.

avatar image huulong · Apr 18, 2015 at 05:31 PM 0
Share

This must be UnityScript's syntax. In any case make sure the collider is enabled and the object active.

avatar image TimK_personal · May 05, 2015 at 10:52 PM 1
Share

Yes, that's UnityScript. Based on ActionScript, which also features that atrocity

avatar image
3

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BrandonHill · Dec 11, 2018 at 01:14 PM 0
Share

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.

avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tomtomhotep · Mar 29, 2017 at 10:46 PM 0
Share

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.

avatar image tony0991 · Jul 08, 2018 at 05:46 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Alternative way of accessing other scripts? 2 Answers

how I can get a property of a GameObject? 2 Answers

Destroy all GameObjects EXCEPT some... 2 Answers

Errors when trying to enable script on gameobject. 0 Answers

How to order newly created Objects? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges