- Home /
The question is answered, right answer was accepted
How can I set the position and size of a sprite from inside a script ?
Hello, Unity Community.
I am currently developing a 2D game, and I want to know if it's possible to set a sprite's position and size from inside a script(C#). I looked at the Sprite.Create entry in the scripting API, but it lacks an example and it only helped confuse me.
It would help me design levels much easier by doing math rather than designing levels by eye in the editor.
You change the position and size of sprite just like you change the position and size of any gameObject in Unity. Change the transform's position and scale values. So yes you can access these in scripts.
Answer by robusto · Jun 14, 2014 at 02:05 PM
It is possible.
public float width = 1;
public float height = 1;
public Vector3 position = new Vector3( 10, 5, 0 );
void Awake()
{
// set the scaling
Vector3 scale = new Vector3( width, height, 1f );
transform.localScale = scale;
// set the position
transform.position = position;
}
Follow this Question
Related Questions
Implementing 2D Sprite Collisions 1 Answer
2 problems when workin with sprites on Unity3D 1 Answer
Recoloring animated sprites [Solved] 2 Answers
Size limits on sprite sheet? 1 Answer
Rotate the weapon sprite with a joystick 0 Answers