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!