Search Unity

Resetting ui element recttransform left, right, top and bottom values

Discussion in 'Editor & General Support' started by Gazehyena, Jul 4, 2017.

  1. Gazehyena

    Gazehyena

    Joined:
    Nov 8, 2016
    Posts:
    3
    Hi,
    For few days already I have been having a problem. When I set an ui element to be another ui elements child, the child elements position get's messed up completely, and it is then located in a very wrong place. I'd want it to be at same location as it's parent, and I can do it by setting in the editor the left, right, top and bottom values of it's rect transform to be all 0. But I cannot do it in code.
    I've tried doing this:

    childItem.transform.SetParent(parentItem.transform, false);
    RectTransform myItemRect = childItem.GetComponent<RectTransform> ();
    myItemRect.anchorMax = new Vector2 (0.9f, 0.9f);
    myItemRect.anchorMin = new Vector2 (0.1f, 0.1f);
    myItemRect.offsetMin = new Vector2(0, 0);
    myItemRect.offsetMax = new Vector2(0, 0);

    This way I can set anchors as I want, but offset does absolutely nothing. (Also I've tried that on SetParent function the boolean value is also true, but it changes nothing.
    The thing is that I want to drag an item from so called talenttree into a hotbar, create a copy of this talent and set it into hot bar (to this point everything works). Then I want to set the original talent back into talenttree to the slot where it was, and in hierarchy view everything is set as it's supposed to, jsut those left, right, top and bottom values are messed up. If someone knows how to reset them to 0, I'd be really happy to hear a way. Right now I'm at a point of starting to lose my interest in unity because of this whole tiny problem.
     
  2. cabbyU

    cabbyU

    Joined:
    Sep 21, 2016
    Posts:
    18
    This is exactly the problem I'm having. Did you ever find a solution for this?
     
  3. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    579
    It would be easier to have a prefab for a talenttree item, and for hotbar item, and then instantiate which one is needed and inject the sprite+text from the source.
     
  4. puchingeralexander

    puchingeralexander

    Joined:
    May 18, 2019
    Posts:
    1
    I had the same error.
    In my case I had to change:
    panel.transform.SetParent(myCanvas.transform, true);
    to
    panel.transform.SetParent(myCanvas.transform, false);