- Home /
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.
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" );
}
Your answer
Follow this Question
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