GUI Button screen Width and Centering Problem

Currently I am having some trouble with my GUI button display. First i can’t see the button when i play without maximizing, button don’t render in the middle of the screen or in other words i can’t even see it, and even if I switch to a widescreen or change the resolution, though i could see the button but its on the left hand side. Is their a solution for this…?

The code is as under:

**var customSkin:GUISkin;
var buttonW:int = 100;
var buttonH:int = 50;
var halfScreenW:float = Screen.width/2;
var halfButtonW:float = buttonW/2;

function OnGUI () {

GUI.skin = customSkin;

if(GUI.Button(Rect(halfScreenW-halfButtonW,560,buttonW,buttonH), “Play”))

{
Application.LoadLevel(“game”);
}**

If that’s all your code, halfScreenW is public, which means it doesn’t change unless you change it yourself in the inspector. If you make halfScreenW a private var, I believe it will update correctly when you start the game.

I find that it’s more flexible to just calculate the screen coords every frame so that when the resolution changes, weirdness doesn’t ensue:

function OnGUI() {
    GUI.skin = customSkin;
    if( GUI.Button( Rect(
            (Screen.width - buttonSize.x) * 0.5f,
            (Screen.height - buttonSize.y) * 0.5f,
            buttonSize.x
            buttonSize.y
        ))
     ) {
        Application.LoadLevel("game");
    }
}

Let me check it out and i,ll get back to you…

Oh… Declaring both as private vars did the trick… but can you just a bit explain … HOW?