Problem
I have a script on a button.
I’d like to find / target the Text inside of that button, and change its RectTransform Values.
Some additional info
I noticed that the fields change based on your anchors so assume all anchors and pivot values are set to “0.5”. If they are, the fields are:
Pos X, Pos Y, Pos Z, Width, Height
Thanks for your help.
Default Unity button has one child which is that Text object. You can access it in many ways as any other object within Unity scene. You can create public RectTransform TextObject
in button script, then drag and drop the child to the field. You can create private RectTransform TextObject
and in Awake/Start method of button script call TextObject = GetComponentsInChildren<RectTransform>()[1]
(Note that first child of an object is the object itself!).
Anyway, after that operation you can manipulate position of an object using anchoredPosition. See the link for example:
https://docs.unity3d.com/ScriptReference/RectTransform-anchoredPosition.html
@Sonky108 Thanks for this. The second option was exactly what I was looking for because I wanted a script that just works whenever it’s dropped on a button without having to manually link. Your note was very helpful as well as I never would have guessed that the [0]th child was the button.
Cheers