Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

AnchorPosition not working

Discussion in 'UGUI & TextMesh Pro' started by keiranlovett, Nov 11, 2014.

  1. keiranlovett

    keiranlovett

    Joined:
    May 9, 2013
    Posts:
    16
    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<RectTransform>();
    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.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892