- Home /
How to change RectTransform.pivot through script?
Hello. I was just wondering how to change that pivot for the RectTransform.
I tried checking the docs but I couldn't quite understand how to put it together.
gameObject.GetComponent<RectTransform>().pivot.x;
this is the best I got, but it doesn't work.
Well anyways ultimately i'm trying to make my pivot's position enter my Input.mouseposition but that's another thing.
I'd appreciate it if someone could kindly tell me how to change the pivot of a rectTransform through scripting.
: )
Answer by rlo · Oct 21, 2015 at 06:04 AM
Try this:
gameObject.GetComponent<RectTransform>().pivot = new Vector2(mousePosition.x, mousePosition.y);
notice that pivot only accepts Vector2.
Thanks Dude, this actually worked.
I guess I cant get it by the Dot X or Dot Y Pivot.
And thanks for the tip, that pretty much made sense.
Answer by farshids · Aug 19, 2021 at 02:58 PM
Also you can use this which should be a little faster instead of using GetComponent:
(gameObject.transform as RectTransform).pivot = new Vector3(mousePosition.x, mousePosition.y);
Your answer
![](https://koobas.hobune.stream/wayback/20220612042103im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change RectTransform pivot without changing position 0 Answers
How to force counter-adjusting of RectTransform Position when changing the Pivot at runtime? 1 Answer
Set a RectTranform's pivot without changing its position 3 Answers
Changing Pivot of rotated RectTransform makes the Object jump to an incorrect Position 1 Answer