GUI button

Hi everyone!

I’ m an absolute beginner to Unity and scripting. I’ ve been following along to a tutorial book for Javascript and am at the GUI button portion. I follow the code example exactly but am having issues with the placement of my GUI button. I am trying to place the button in the centre of my screen, but when I push play to test the button appears lower left centre area of my screen I’ve taken a screen shot to demonstrate. I’ve looked for answers through google but have not find a solution to my problem. I even looked at the scripting reference but do not believe I found anything there to help me.

Here is the code for it.

function Start () {

}


	function OnGUI() {
var customSkin : GUISkin;
//button width
var buttonW : int = 100;
//button height
var buttonH : int = 50;
var halfScreenW : float = Screen.width /2; //half the screen width
var halfButtonW : float = Screen.height /2; //half the button width
	GUI.skin = customSkin;
		
		if (GUI.Button(Rect(halfScreenW - halfButtonW, 560, buttonW, buttonH),"Play Game"))
			Debug.Log("Clicked the button with text");
	}

A button takes the parameters(x position of left upper corner, y position of left upper corner, button width, button height)

So what you’re looking for is

 if (GUI.Button(Rect(halfScreenW  - buttonW/2, halfButtonW, buttonW, buttonH),"Play Game"))

The -buttonW/2 part moves the button back half the width so it remains centered.

And rename halfButtonW to halfScreenH :slight_smile:

Try this :

function OnGUI(){

if(GUI.Button (new Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, buttonW, buttonH),"Play")){
Debug.Log("Clicked the button with text");
}
}

Thank you!