Reputation: 117
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
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:
Upvotes: 0
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
Reputation: 8163
Yes, it is done through RectTransform obj
obj.localPosition = new Vector3(0f,0f,0f);
Upvotes: 1