Button Position Problem

I’m trying to add a new button with an image to an existing canvas in a button callback.

I’m using the following code, where this is the existing canvas. The size and image are perfect but the recttransform position ends up at -177.5, -27.5 in the debugger. If I change its position in the debugger to 160, 175 it is fine but this isn’t working in code.

GameObject buttonObject = new GameObject ( “Assigned Garage Button” , typeof ( RectTransform ) , typeof ( Image ) , typeof ( Button ) );
buttonObject.transform.SetParent ( this.transform, false );
RectTransform buttonTransform = buttonObject.GetComponent ();
buttonTransform.position = new Vector3 ( 160.0f , 175.0f , 0.0f );
buttonTransform.sizeDelta = new Vector2 ( 160.0f , 30.0f );
Image assignedGarageBar = buttonObject.GetComponent ();
assignedGarageBar.sprite = GUIHeader_Sprite;

Thanks for your help!

Are you sure you’re not trying to set localPosition?

The posted code is all of what I’m setting and I’m looking at the recttransform for Assigned Garage Button in the debugger. Changing that to 160.0, 175.0 in the debugger is what puts the button where I want.

I’m wondering if I’m missing an attribute. I noticed the button created in the editor ( positioned at 0.0, 175.0) has the layer set to UI and my Assigned Garage Button does not. Otherwise the two look the same.

That is my question. Are you sure you don’t want to set localpostion instead of position?

Another thing that might be possible is you’re changing the size after moving it. Might switch those around and see if that helps. Sometimes changing the size can change the position depending on anchors and such. Just a thought.

Local position was what I needed. Thanks much!