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 ();
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.