- Home /
The question is answered, right answer was accepted
Are my bounds right world position in this case ?
I use script on prefab to instantiate many block to make rooms.
I use a script to make bouds = of all instantiated children block of piece.
My debug give good value ( i think ) in console.
But is the white draw good value ? Why all go to 0 0 0 world ?
Script is here :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bound : MonoBehaviour {
void OnDrawGizmos()
{
Bounds bounds = GetChildRendererBounds(gameObject);
Gizmos.DrawWireCube(bounds.center, bounds.size);
Debug.Log(bounds +","+ gameObject);
}
Bounds GetChildRendererBounds(GameObject go)
{
Renderer[] renderers = go.GetComponentsInChildren<Renderer>();
if (renderers.Length > 0)
{
Bounds bounds = renderers[0].bounds;
for (int i = 1, ni = renderers.Length; i < ni; i++)
{
bounds.Encapsulate(renderers[i].bounds);
}
return bounds;
}
else
{
return new Bounds();
}
}
}
Ok my bounds are wrong in console to, center is center of bounds we show with white draw.
Answer by Bunny83 · Jul 23, 2017 at 09:08 PM
Your combined bounds look like they somehow include the world origin as well. However your code specifically avoids this since you correctly start with the bounds of the first and then combine with the remaining.
I think your problem is that your parent object also has a MeshRenderer but that doesn't seem to have any mesh. That's why the bounds of the parent most likely is an empty bounds located at worlds "0,0,0".
"GetComponentsInChildren" also returns components on the actual object, not only components on children. So you also process the MeshRenderer on your parent object.
Since you don't have a MeshFilter with an actual mesh to render on your parent you might want to simply remove the MeshRenderer from your parent as it serves no purpose besides causing trouble.
Thanks so mutch ! That was that. I have delet on my script the mesh add component to the parent, and my bounds are all good.
Follow this Question
Related Questions
Dynamically creating a box collider for an object with multiple, rotated, and scaled mesh renderers 0 Answers
Terrain Generator Problem: Index is less than 0 or more than or equal to the list count 1 Answer
Getting Collider2D.bounds.extents to update immediately (2D) 0 Answers