S
S
Surface-ketch2016-03-30 15:35:00
C++ / C#
Surface-ketch, 2016-03-30 15:35:00

How to get width value of 2d object in Unity?

How to get the width of an object? LocalScale.x returns one all the time. In Rect Transformation there is width = 19, if I manually substitute it into the code, then everything works as it should, but how to get this value through a variable?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Basmanov, 2016-03-31
@Surface-ketch

The width of the RectTransform can be obtained from RectTransform.rect :
float width = rectTransform.rect.width;

G
GreatRash, 2016-03-30
@GreatRash

Vector2 size = GetComponent<SpriteRenderer>().sprite.rect.size;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question