|
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
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?
(comments are locked)
|
|
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: 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)
|
