- Home /
GUI button creation and placement
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");
}][1]
[1]: /storage/temp/19505-gui+button.png
Answer by Stormizin · Dec 18, 2013 at 04:46 PM
Its because you are setting your button to the position:
Rect(half - half, 560, 100, 50)
You need to do this:
GUI.Button(Rect(Screen.width/2, Screen.height/2, buttonW, buttonH), "Play Game!");
Or:
halfScreenH = Screen.height/2;
GUI.Button(Rect(halfScreenW, halfScreenH, buttonW, buttonH), "Play Game!");
Hope i helped ya!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Knowing when a button is pressed, not released. 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Material doesn't have a color property '_Color' 4 Answers
Setting Scroll View Width GUILayout 1 Answer