Rendering GUI.Buttons in the middle of the screen

Hey I am having some trouble with my GUI. In my main menu they buttons render in the middle of the screen, but if I switch to a widescreen or change the resolution the buttons move and sometimes you can't even see them. Does anyone know how to fix this?

Depends on how you placed your buttons. With GUIText and GUITexture, the "Pixel Offset"/"Pixel Inset" parameters are problematic, since they are pixel based, and therefore, placement of your GUI elements will depend on your screen resolution.

I wrote a little wrapper script, which normalizes screen positions to -0.5..0.5, and also takes care of the aspect ratio, and translates these values to pixel offsets depending on your screen resolution. This way, you can place your GUI elements consistently for any screen size.

In essence, you need to do something like (C# example for GUIText):

public float X;
public float Y;


    float Xcoord=X*Screen.width;
    float Ycoord=Y*Screen.height;

    // do we have a GUIText?
    GUIText guiText=GetComponent<GUIText>();
        // translate to normalized position
        guiText.pixelOffset=new Vector2(Xcoord,Ycoord);

I usually just grab the width and height from the Screen.width and Screen.height. This gives me the current resolution. You can do this in the top of your OnGUI even or just store them in variables that you update when you switch resolution.

To center elements, you’ll need to know the width and height of your element to center.

Formular is rather simple:

int xpos = (Screen.width)-(this_element.width)/2;
int ypos = (Screen.height)-(this_element.height)/2;

With Screen.width you can access the width of your screen in pixels. Divide that by half and you have the exact middle of your screen. Position the buttons at the appropriate spot by taking a certain percentage of the screen width (e.g. placing them around 45% of your screen width). That should keep them in the middle no matter what your resolution is.

In C# Script:

if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 50)
    //Enter Code here

try this one ! for square button

if (GUI.Button (new Rect ((Screen.width/2)-(Screen.width/4), (Screen.height/2)-(Screen.width/4), Screen.width/2, (Screen.height-(Screen.height-Screen.width))/2),"",Question)) {