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?

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

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