Rupert Stamp
Rupert Stamp

Reputation: 117

Move Ui.Text Unity

I want to move a UI.Text component during runtime via script (in my games circumstance I want to move the player's score to the center of the screen at death).

How can you move UI.Text via script? Is it done through RectTransform?

Upvotes: 0

Views: 5346

Answers (3)

user9722505
user9722505

Reputation: 1

You can move any UI element using script with a coroutine and a MoveTowards method or Lerp, without the disadvantages of using Animator.

I explain how exactly make that in this tutorial:

https://youtu.be/LBoPP9mKjKc

Upvotes: 0

Reasurria
Reasurria

Reputation: 1838

You might want to try obj.anchoredPosition or obj.anchoredPosition3D instead. This will position the UI element relative to it's current anchors. So it's easier to predict where the element will appear. This position will correspond with the position you set in the inspector so you can use the inspector to test and find the position you like.

For example if the text is a child of the main canvas and it has a center anchor and pivot, setting anchoredPosition to Vector2.zero will set it to the center of the screen.

Upvotes: 1

maraaaaaaaa
maraaaaaaaa

Reputation: 8163

Yes, it is done through RectTransform obj obj.localPosition = new Vector3(0f,0f,0f);

Upvotes: 1

Related Questions