Screen.width/2 Giving an Incorrect Value

I’m trying to declare a variable to set the position of a button as ‘Screen.width/2’. When doing so, Unity gives the value of the variable in Inspector view as 154 whereas the Screen’s width in Maximize on Play mode is about 909 so the button is positioned too far to the left.

I have used ‘Screen.width/2’ before in a variable, yet it was in a function whereas this time I am declaring the variable outside of the function.

What could I be doing wrong?
(Also, sorry for any missing details, I’m fairly new to this whole Unity thing!)

Edit: Of course, the code would probably be helpful, sorry about that:

#pragma strict

function Start () {

 //button width
var buttonW : int = 100;
//button height
var buttonH: int = 50;

//Half of Screen width
var halfScreenW : float = Screen.width/2;
//Half of Button width
var halfButtonW : float = buttonW/2;   
//Declare variable 'customSkin' as type GUISkin
var customSkin : GUISkin;

function OnGUI () {    	
	//Set variable 'customSkin' as the main (customSkin is assigned in Unity Inspector) = customSkin;
	//Create button and detect if clicked. Creating button at midpoint of screen so that the midpoint of button is directly at midpoint of screen. Y is set to 560 as it drops below the title image.
	if(GUI.Button(Rect(halfScreenW-halfButtonW,560,buttonW,buttonH),"Play Game"))
		//print("You clicked me!");


As you can probably tell, it is tutorial sample code so I probably haven’t fully understood it.

When are you assigning the value to that variable? in Start()? It is possible that the screen width doesn’t have the maximized value there and the variable doesn’t get updated.

Are you setting the variable at the same time you declare it? If so, and it’s a public property that shows up in the inspector, whatever the initial value was when you added the script will persist because it’s serialized that way.

If you want the value to change based on the actual screen size you should set it in the Awake or Start functions.