How to move a RectTransform on Pos X through code

I’ve spent a good amount of time trying to work this out. I haven’t read any posts that clarify this issue, and the documentation is completely baffling to me.

So, how do you move a RectTransform on the Pos X through code? I know how to edit the width and height of a panel or image, but I have no idea how to move it on the x or y. I’ve tried a lot of different things and got weird results. I suspect it has something to do with the pivot of the panel?

ah, instead of this:

playerInventoryRectTransform.localPosition = Vector3.zero;

I should have used this:

playerInventoryRectTransform.anchoredPosition = Vector3.zero;

It works now, but readers let me know if I’m still missing something.

I had a similar problem. I was trying to instantiate a prefab and then have it populate the parent object.
I could not move it using localPosition or change the size using sizeDelta. To fix the issue, I used RectTransform.SetInsetAndSizeFromParentEdge().

See example code below:

// GameObject obj is the parent object;

    GameObject togglePrefab = Resources.Load("Prefabs/Toggle") as GameObject;
    GameObject toggle = GameObject.Instantiate(togglePrefab, obj.transform) as GameObject;
    RectTransform rt = toggle.GetComponent<RectTransform>();
    rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, rt.rect.width);
    rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, rt.rect.height);

You can modify its local position.

RectTransform myRectTransform = GetComponent<RectTransform>();
myRectTransform.localPosition += Vector3.right;

I’ve gone completely crazy before getting a solution. xD

I was looking for a system to set the Anchor on the Top-Left, and passing value like:
X; 10, Y: -10 Z: 0

So I’ve made a method to use in a for/loop cycle, I will pass a GameObject and a Vector3 with the same parameter just like I would do in inspector.

void tfSetup(GameObject go, Vector3 pos)
	{
		go.AddComponent<RectTransform>();
		go.AddComponent<TextMeshProUGUI>();
		go.AddComponent<TMP_Text>();

		RectTransform rt = go.GetComponent<RectTransform>();
		rt.SetParent(goParent.transform);
		
		rt.anchoredPosition.Set(0, 0);
		rt.anchorMin.Set(0, 1);
		rt.anchorMax.Set(0, 1);
		rt.pivot.Set(0, 0);

		rt.transform.localPosition = pos;

	}

RectTransform rT = GetComponent();

rT.anchoredPosition = new Vector2(100f, 300f); // Sets the position to 100, 300

rT.anchoredPosition += new Vector2(1f, 3f); // Changes the position about 1 at the X-Axis and 3 at the Y-Axis

For me this code works quite well:

public void MoveLeft(RectTransform panel)
	{
		StartCoroutine(Move(panel, new Vector2(-1255, 0)));
	}

	IEnumerator Move(RectTransform rt, Vector2 targetPos)
	{
		float step = 0;
		while (step < 1)
		{
			rt.offsetMin = Vector2.Lerp(rt.offsetMin, targetPos, step += Time.deltaTime);
			rt.offsetMax = Vector2.Lerp(rt.offsetMax, targetPos, step += Time.deltaTime);
			yield return new WaitForEndOfFrame();
		}
	}

The only note here is that I had to change the panel parameter from a ui panel to an empty game object for this to work properly.

For me this code works quite well: public void MoveLeft(RectTransform panel) { StartCoroutine(Move(panel, new Vector2(-1255, 0))); } IEnumerator Move(RectTransform rt, Vector2 targetPos) { float step = 0; while (step < 1).