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.

: )

Try this:

gameObject.GetComponent<RectTransform>().pivot = new Vector2(mousePosition.x, mousePosition.y);

notice that pivot only accepts Vector2.

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);