Getting the width of a sprite

Vaibhav picture Vaibhav · May 8, 2014 · Viewed 46.6k times · Source

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

MBehtemam picture MBehtemam · Aug 21, 2015

If you are using Unity 5 you should use this code:

float tileWidth = tileSet[0].GetComponent<SpriteRenderer>().bounds.size.x;

Pay attention to your pixels per unit.