# How can I make the scale of my sprite in the game view match its actual size in pixels?

For example, I have an image that is 150x150 pixels. When I place it as a sprite in the game view, how can I match its unity scale with its pixel size? I need to know to preform calculations with its size on run time in code.

Hopefully I didn’t misunderstand your question.

You will have to get the size of the bounding box of the sprite (which is in unity units).

Then mulitply it by the sprite’s pixelperUnit(this is set when you import a sprite default is 100) and set the scale of the object.

``````// get  a reference to your spriterenderer
SpriteRenderer render = GetComponent<SpriteRenderer>();

//bounds is convenient to make rough approximations about the object's location and its extents
transform.localScale = render.bounds.size * render.sprite.pixelsPerUnit;
``````

To find the min and max values you can check the bounds. these values take into consideration scaling.

``````render.bounds.min
render.bounds.max
render.bounds.extents
``````

using that information you should be able to randomly spawn sprites.

You could do something like this:

SRenderer is the spawned object’s spriterenderer.

I am taking into consideration the size of the sprite you are spawning.

``````    float xVal= Random.Range(BGRenderer.bounds.min.x+SRenderer.extents.x,BGRenderer.bounds.max.x-SRenderer.extents.x);

float zVal= Random.Range(BGRenderer.bounds.min.z+SRenderer.extents.z, BGRenderer.bounds.max.z-SRenderer.extents.z);

//I am using 0 for y as you mentioned its a topdown(so I am assuming your camera is rotated 90 degrees on the x).
Vector3 RandomSpawnPoint = new Vector3(xVal,0,zVal);
``````

hopefully that helps.