GUI buttons

I have a start menu with buttons on it the code is fallowing this. What im having a problem with is geting it to stay in place as the screeen resolution changes on the computers.

function OnGUI () {

if (GUI.Button (Rect (400,500,200,40), "Exit Game")) {






Well, you haven’t really told us what you actually want it to do, only what it’s doing now! I’m afraid it currently does exactly what you are telling it to do.

If you want the button to always be in the middle of the screen, you can do something like this-

var pixelWidth : float;
var pixelHeight : float;
var viewportCentre : Vector2;

function OnGUI()
    if(GUI.Button(Rect((Screen.width * viewportCentre.x) - (pixelWidth / 2), 
                       (Screen.height * viewportCentre.y) - (pixelHeight / 2),
                       pixelWidth, pixelHeight), "Exit Game"))

Set up the three values in the inspector, and this will give you a button that is always pixelWidth wide and pixelHeight high, but sits nicely in the centre of the screen!

Otherwise, just use GUILayout for everything (so that it happens automagaically).

Here’s a similar question from a couple weeks ago: