x


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?

more ▼

asked Apr 22 '10 at 10:05 AM

headkit gravatar image

headkit
280 25 28 36

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
    }
}
more ▼

answered Apr 22 '10 at 10:11 AM

duck gravatar image

duck ♦♦
40.9k 92 148 415

thank you for your answer! looks quite interesting. but i got the error:

MissingComponentException: 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.

Apr 22 '10 at 12:43 PM headkit

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...

Apr 22 '10 at 12:56 PM headkit

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)

Apr 22 '10 at 01:09 PM headkit

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 min and max point of each child renderer's bounds.

Apr 22 '10 at 01:12 PM duck ♦♦

goo idea, thank you!

but what about the possibility to find a renderer with prefab.GetComponentInChildren(Renderer); does that work?

Apr 22 '10 at 01:15 PM headkit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2074
x1668
x1250
x286
x94

asked: Apr 22 '10 at 10:05 AM

Seen: 4137 times

Last Updated: Apr 22 '10 at 12:59 PM