# Unity UI can be hard

## Rect Transforms

Unity has many transforms that you use for game objects; however, Unity uses the RectTransform for UI.  There are many things you can do: set Anchors and Pivots. &#x20;

![Unity's Rect Transform](https://1970861158-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ljn7Z64iRi1UjaYmuws%2F-LuoZ0_v1v0GqwSdFJX5%2F-Luo_2v7GRHiuznh_eK7%2FAnnotation%202019-11-29%20103332.png?alt=media\&token=a1ca841c-bb2e-45da-960f-f23668fcb4ac)

### ScreenWidth

I've used Screen.width before to get the width of the screen and set a variable to the width of the screen.  Sometimes, this won't work 'causing all the UI elements to .  So, you gotta get creative. &#x20;

Say you have a button that's not playing nicely.  You can create an empty gameObject in the canvas and call it ButtonParent.  Put the Button in the ButtonParent like below. &#x20;

![](https://1970861158-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ljn7Z64iRi1UjaYmuws%2F-LuoZ0_v1v0GqwSdFJX5%2F-LuohU3tGAfaWHQXXcYj%2FAnnotation%202019-11-29%20111030.png?alt=media\&token=45a16a9c-e32b-4c3c-b80d-b1f8b85a2f1d)

In your code, you want to create a RectTransform variable and in editor, drag the ButtonParent to reference it as the parentRectTransform. &#x20;

Next, instead of using, Screen.width as your width, use parentRectTransform.rect.width. &#x20;

```
using UnityEngine;

public class ButtonTest : MonoBehaviour {
    [SerializeField] private RectTransform parentRectTransform;
    
    void Start() {
        float screenWidth = parentRectTransform.rect.width;
        // int screenWidth = Screen.width;
    }
}
```
