Help With Alignment

I’m trying to align a image and a text with this code:

 parentAnchorX = imageTransform.anchoredPosition.x + textTransform.anchoredPosition.x / 2;

        parentTransform.anchoredPosition = new Vector2(parentAnchorX, parentAnchorY);
        parentTransform.position = new Vector3(parentTransform.position.x - parentAnchorX * 2,
                                                parentTransform.position.y, parentTransform.position.z);

I know this doesn’t look right, this is just what I got to. The arrows you use to move the object aren’t on the position of the object(the image and the text i’m trying to center, this script is on a parent game object). I just don’t understand how it’s possible to center these objects.

Everything I wrote here still applies:

Manipulating RectTransforms is EXTREMELY tricky.

Alright my bad

No problem, I went through a phase where I made great progress manipulating UnityEngine.UI stuff by code, and then I slightly changed something (like the scaling mode, or render mode, or even the aspect ratio of the screen, I forget what), and freaking EVERYTHING that my code relied on didn’t work anymore. I chased it for a while and then just gave up when I had pages and pages of calculations and every time I ran it I got different results. Maddening.

Of course at the end of the day it’s just code and data, but as you can see by the 15+ steps required to set anchors, I just want to point out that it is really hard to get right. You basically have to get into the minds of the developers of UnityEngine.UI, and they did some crazy stuff in there anchoring-and-scaling-wise.