Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by jdwieber · May 31, 2012 at 11:19 PM · sizeboundslodgroup

Get bounds.size of a game object in an LODGroup?

Hello,

How can I get the bounds size of a game object in an lod group? I tried modifying code that I found here: link text

Snippet of my C# script:

     //Code to assign m_wall the appropriate prefab is done elsewhere
 
     Vector3 size = Vector3.zero;
     LODGroup lodGroup = m_wall.GetComponent< LODGroup >();
     if( lodGroup )
     {
         SerializedObject obj = new SerializedObject( lodGroup );
         SerializedProperty prop = obj.FindProperty( "m_LODs.Array.data[0].renderers" );
         SerializedProperty propRenderer = prop.GetArrayElementAtIndex( 0 ).FindPropertyRelative( "renderer" );
         size = propRenderer.boundsValue.size;  // <--error message points to this line
     }
     else
     {
         size = m_wall.renderer.bounds.size;
     }
     m_wallWidth = size.x;
     m_wallHeight = size.y;
     m_wallLength = size.z;

but it doesn't work. I get the following error at runtime:

 Mismatched types in GetValue - return value is junk
 UnityEditor.SerializedProperty:get_boundsValue()

Am I allowed to use stuff from the UnityEditor namespace from a game script? In my game, I build levels on the fly using different prefabs. Some are simple walls, and others are complex and have an lod group. The simple walls work fine. AFAIK, I only need to get the bounds of the first lod level as the lesser levels have meshes that are the same dimensions. Can someone please tell me how to do this? 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

1 Reply

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

Answer by jdwieber · Jun 11, 2012 at 06:01 PM

Ok, so I found a simple way to do this. In my case, the object m_wall is a prefab in a script set through the inspector, so I need to instantiate a dummy object first. Then to get the bounds I get a renderer from one of the children by calling GetComponentInChildren and pull the bounds from the renderer. Here is a code snippet from my game:

     GameObject dummy = (GameObject)Instantiate( m_wall, Vector3.zero, Quaternion.identity );
     Renderer renderer = dummy.GetComponentInChildren< Renderer >();
     if( renderer != null )
     {
         Vector3 size = renderer.bounds.size;
         m_wallWidth = size.x;
         m_wallHeight = size.y;
         m_wallLength = size.z;
         Destroy( dummy );
     }
     else
     {
         Destroy( dummy );
         throw new System.Exception( "Failed to get renderer from wall object" );
     }

EDIT: For sake of completeness, the above only works for a single object. I have other prefabs that I build by combining multiple prefabs together under a single parent. In that case, things go horribly wrong. When I use those models I have to manually calculate the bounds and so the code for that looks like this:

     GameObject dummy = (GameObject)Instantiate( m_wall, Vector3.zero, Quaternion.identity );
     Renderer[] renderers = dummy.GetComponentsInChildren< Renderer >();
     if( renderers.Length >= 1 )
     {
         Debug.Log( "Found " + renderers.Length + " renderers" );
         Bounds bounds = new Bounds( Vector3.zero, Vector3.zero );
         foreach( Renderer r in renderers )
         {
             bounds.Encapsulate( r.bounds );
         }

         Vector3 size = bounds.size;
         m_wallWidth = size.x;
         m_wallHeight = size.y;
         m_wallLength = size.z;
         Destroy( dummy );
     }
     else
     {
         Destroy( dummy );
         throw new System.Exception( "Failed to get renderers from wall object" );
     }
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

LodGroup manually set Bounds 0 Answers

Print bounds size. 3 Answers

Can I use a raycast to retrieve information from the collider? 2 Answers

Is the bounds.size and the GetComponent.BoxCollider2D.size of a GameObject meant to be the same? 1 Answer

TextMesh.renderer.bounds.extents not 100% accurate? 2 Answers


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