with var and without var creates different effects

Hi everyone, I'm a newbie to unity3d and programming in general. just started a few weeks ago and i'm still trying to figure things out. Right now I'm following a book on unity3d and I've stumbled on something strange that you might be able to help me with.. This is the code I'm using to create a button and place it in the center of the screen:

var customSkin:GUISkin;

var buttonW:int = 100;

var buttonH:int = 50;

private var halfScreenW:float = Screen.width/2;

private var halfButtonW:float = buttonW/2;

function OnGUI(){


    if(GUI.Button(Rect(halfScreenW,560,buttonW,buttonH),"Play Game"))

        print("You clicked me!");

One thing I've noticed is that when I replace "halfScreenW" with the actual value of the private var, "Screen.width/2", I get a different result than if I use the var name.. so the button's x value is different.. can someone tell me why this is and how to solve it?..

I've also noticed this with simple scripts like values multiplied by Time.deltaTime.. I would get a different result if I use a var as opposed to inputing an actual number.. Any help/explanation is highly appreciated!

It's because when you run the program the size of the screen changes.

If you simply type Screen.width/2 you'll always get a value that's half the screen width even if you change the size of the screen while it's running.

On the other hand if you use a stored value like with a declared variable it's static and wont change with the screen size at run time.

You can test this fairly easily by creating 2 boxes with each method and dragging the screen width around. It's not really a bug, just use the method that you think is right for you needs.