AnchorPosition not working

I am writing a script that allows us to write UI elements at runtime, however we have an issue where the anchorPosition isn’t positioning elements properly.

“My Button” is a child of SubGroup attached to Panel. When we test the code, everything generates fine, but the position of SubGroup isn’t at 0,0 relative to the panel.

public static RectTransform BeginSubControl(string name = “SubGroup”) {
GameObject go = new GameObject(name);
RectTransform t = go.AddComponent();
if (subControls.Count > 0)
{
t.parent = subControls.Peek();
}
subControls.Push(t);

t.anchoredPosition = new Vector2(0, 0);

return t;
}
If I manually go in and adjust anchorPosition from the inspector at runtime to 0,0 it positions dead center of the parent though.

You’re saying that anchoredPosition isn’t at 0,0 after running this line?

t.anchoredPosition = new Vector2(0, 0);

In any case, you’re setting the parent using the .parent property. I would recommend reading this page, particularly the note about SetParent: