- Home /
Get size of Transform
Hi, I am developing a game consisting of a tile map. This map is based on multiply UnityEngine.Transform objects. Each Transform is loaded from a prefab, which is consisting of a GameObject with a sprite inside.
I am instantiating the Transform with this code:
float tileSize = 1f;
for(i = 0; i++; i < mapSizeX)
{
for(... i < mapSizeY)
{
Transform tile = Instantiate(map[x,y], new Vector3(x * tileSize, y * tileSize, 0), Quaternion.identity) as Transform;
tile.parent = transform;
}
}
This is working great, but the float value, tilesize, should be as large as the size of the transform. Is there another way for getting the size of every tile?
I tried it with transform.renderer.bounds.size, but this always returns 0. Does anyone has an idea?
thanks, Christian
$$anonymous$$ore than renderer use collider.bounds.size or you can also read lossy or local scale values
Answer by Baste · Mar 08, 2015 at 11:41 AM
The size of a transform isn't really a very well-defined concept. Do you want the volume? Surface? height?
When you ask for the bounds of either the renderer or the collider, what you get is an Axis Aligned Bounding Box (AABB for short). This is a box that lies along the x, y and z axes, and that contains the entire renderer or collider.
So when you do transform.renderer.bounds.size, you'll get a Vector3 that represents the width, height and depth of the bounding box. The same thing goes for collider.renderer.bounds.size.
So, the tilesize is only viable if you have square tiles. Otherwise, you'll want renderer.bounds.x and renderer.bounds.y to give you tilesizeX and tilesizeY, and multiply those with the coordinates.
If you're getting Vector3.zero from the renderer, it probably doesn't have a mesh it's drawing. There might also be some issues with reading off the bounds of something that's not loaded in yet - like a prefab. Make sure you're actually treating the size like a Vector3, and if the renderer's bounds isn't working, grab the collider or mesh bounds instead.
Your answer
![](https://koobas.hobune.stream/wayback/20220613185309im_/https://answers.unity.com/themes/thub/images/avi.jpg)