- Home /
How can I determine an object of prefab's size (for positioning when instantiating them)
hi all!
new to unity and very exitet! my first question: i want to instatiate objects besides each other and need to know their individual width in the direction they could possible overlap. actually i want to add a variable called "space" for the gap inbetween the positioned objects.
f.e. if i want to position the objects in a row like
function setCards_Row ()
{
for (var i=0; i<num_of_Cards; i++) {
Instantiate (prefab, Vector3(i * spaceBetween, 0, 0), Quaternion.identity);
}
}
so how do i get the specific width of an gameobject in one direction?
thnx!
edited: if the prefabs are Empty's with children of many objects, is there a way to detect the cild with the biggest Renderer-Bound?
Answer by duck · Apr 22, 2010 at 10:11 AM
You can use its renderer.bounds. "bounds" is a struct which contains many useful properties such as size, extents, center, min and max.
Note, however, that these sizes will be for the Un-Scaled size of the object (as if its scale was 1,1,1), and the directions (eg, in the size x,y,z values) will be relative to the objects local coordinates.
To position your objects next to each other with a specified gap between, you might modify your code to look something like this:
function setCards_Row ()
{
var nextPosition = 0;
var gap = 0.5;
for (var i=0; i<num_of_Cards; i++) {
Instantiate (prefab, Vector3(nextPosition , 0, 0), Quaternion.identity);
nextPosition += (prefab.renderer.bounds.size.x + gap);
}
}
thank you for your answer! looks quite interesting. but i got the error:
$$anonymous$$issingComponentException: There is no 'Renderer' attached to the game object, but a script is trying to access it. You probably need to add a Renderer to the game object . Or your script needs to check if the component is attached before using it.
oh, i see. i instatiate an Empty with meshed objects as children. seems to be impossible in that way. so i will need to ask for one of the objects child renderers. hm...
shouldn't it be something like this:
Instantiate (prefab, Vector3(nextPosition , 0, 0), Quaternion.identity); var theRenderer : Renderer = prefab.GetComponentInChildren(Renderer); nextPosition += (theRenderer.bounds.size.x + gap);
(wrong code)
The 'bounds' struct has an .Encapsulate function, which expands the bounds' properties to include the new point. Using this, you could possibly iterate through every renderer of every child object, and call Encapsulate on the $$anonymous$$ and max point of each child renderer's bounds.
goo idea, thank you!
but what about the possibility to find a renderer with prefab.GetComponentInChildren(Renderer); does that work?
Answer by estani-berlin · May 24 at 08:47 AM
In my case I wanted to draw a gizmo where I needed the prefab size, if present. (You can see in your prefab if there's a Renderer component as mentioned by @duck, in my case it wasn't... (isn't this always the case?). It "might" be after instantiation I guess, but I'm not instantiating it in the Editor, so this wouldn't work for me either)
In any case my prefad had a BoxCollider, using it seems to work quite well, and certainly you don't have to instantiate the prefab to access it:
prefab.GetComponent<BoxCollider>().size