Search Unity

Changing the position of a child:

Discussion in 'Scripting' started by All_American, Dec 2, 2019.

  1. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I need to zero out a panel attached to a gameobject and I want to raise it up. Nothing I do moves the panel at all.

    I tried grabbing the RectTransform but that did not work either.

    Code (CSharp):
    1. RectTransform UIpos = attachPanel.GetComponent<RectTransform>();
    2.                 UIpos.transform.localPosition = new Vector3(0, 0, 6.0f);
    I am able to rotate the panel but can not move it.
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,330
    Try RectTransform.anchoredPosition.

    If that doesn't work then the problem could be e.g. that a parent has a LayoutGroup component which is overriding the position.
     
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    .transform. is not needed here, but removing it won't solve the problem, it just not needed. SisusCo is right, if it doesn't move, then something must be holding it back. If you are not sure about this, test that line of code you posted in a empty project. It will work.
     
  4. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    This is how I solved it.

    Code (CSharp):
    1. attachPanel.transform.localPosition = new Vector3(gmeshr.bounds.center.x, gmeshr.bounds.center.y, sizey + 2.0f);
    For some reason when the canvas prefab is instantiated the transforms are all jacked up. z is up and down.