- Home /
Getting the width of a sprite
I am trying to create a row out of some square sprites I have. So to get the width of these sprites i am using
tileWidth = (int)tileSet[0].renderer.bounds.size.x;
And then to form the row i am uisng
for(int i = 0; i < tileSet.Length ; i++){
if((i+1)*tileWidth<screenWidth){
tileSet[i].transform.position = new Vector3(i*tileWidth,0,0);
}
}
But the sprites are still overlapping each other and do not form a proper row. What am i doing wrong here and how can i rectify it?
Answer by jakelong00 · May 08, 2014 at 03:59 PM
The tile was overlapping because i casted the tilewidth to an int whereas it can be a float.
Example : if the sprite is 110X110 pixels and the pixels to units for this sprite is set to 100 then tileWidth would be:renderer.bounds.size.x = 110/100 = 1.1 which in my code would be casted to 1 hence the overlapping.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144526im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
In Sprite Editor, how can I get theese values by code? (screenshot) 1 Answer
How to make a health bar in Unity 5? 0 Answers
Sprite slicing problem 1 Answer
Change GUITexture width 1 Answer
Getting a sprites size in pixels 3 Answers