- Home /
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 GUI.skin. (customSkin is assigned in Unity Inspector)
GUI.skin = 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!");
Application.LoadLevel("game");
}
}
As you can probably tell, it is tutorial sample code so I probably haven't fully understood it.
Answer by Dave-Carlile · Jan 04, 2013 at 05:02 PM
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.
I have set it as I declare it and it does appear in the inspector. I have set it outside of a function and it is being used by the OnGUI function.
You need to set it in one of the functions I mentioned, or calculate in OnGUI. Since they're in public variables they're retaining whatever their initial value was. That's how the editor works - it saves the public properties.
Ah, placing it in OnGUI got it working. I also tried placing it in Start however that gave me an error stating that the variable used in OnGUI didn't exist. I assume that is because when a variable is declared in a function, it can only be used in that function?
Anyway, thanks for your help!
Well, you would still need to declare the variable at the top. You would just set the variable in the function.
// declare it at the top
var halfScreenW : float;
// set it in the start function
function Start()
{
halfScreenW = Screen.width/2;
}
But, doing it all in OnGUI is fine too. It's likely not an expensive calculation.
Ah, I see. Will it take up less system resources doing that way? If so, then I'll keep that in $$anonymous$$d.
Answer by Piflik · Jan 04, 2013 at 05:01 PM
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.
I'm assigning it outside of a function, would that cause the same thing as described?
Probably. Also your variables are public, which means they are exposed in the Inspector, like Dave said. They will have a value assigned when the script first compiles and use that value unless you change it. You should either update that variable whenever the resolution is changed, or use Screen.width directly in your GUI code, without assigning it to a variable first.
Declaring the variable and using it in the OnGUI function got it working, although I'm unsure whether that would update the variable whenever the resolution changes. Thanks for your help with this!
Your answer
Follow this Question
Related Questions
how to access a screen(separate obj in my app). 1 Answer
Prefab instantiate 1 Answer
Loading screen? 2 Answers
Display compatibility between windows 7 and xp 1 Answer
Android fullscreen change 1 Answer