- Home /
Can't work with screen.width / height
This is my code:
function OnGUI () {
var H = Screen.height;
var W = Screen.width;
var Ratio;
Ratio = H / W;
GUI.Label (Rect (10, 110, 100, 20), "Screen.width "+ W.ToString());
GUI.Label (Rect (10, 130, 120, 20), "Screen.height "+ H.ToString());
GUI.Label (Rect (10, 150, 120, 20), "Screen ratio "+ Ratio.ToString());
}
When i run it, it displays proper screen dimensions, but Ratio is always 0. Also when i try to operate on Screen.height or Screen.width (add to them etc.) they become zeros. I followed tutorials and copy/pasted code from documentation - the Screen.width and .height show ok only if i don't try to use them in calculations. This is becoming frustrating, what am i missing here?
This happens both in editor and in build.
Ah, this was exactly the case. I even suspected this could be about the integers, but thought that declaring Ratio as a float would be enough. Which it wasn't. Thank you guys.
Answer by Eric5h5 · May 08, 2012 at 09:07 PM
Integer division always gives an integer result. If you want float division, at least one of the numbers involved must be a float.
Answer by syclamoth · May 08, 2012 at 09:10 PM
This happens because 'Screen.height' and 'Screen.width' return integers! Therefore, the division operator will return 0 in all cases in which the second number is larger than the first. Try this simple modification to your code (and lament the fact that UnityScript even allows you to make mistakes of this kind- if it was statically typed you'd have picked this one up immediately...)
var H : float = Screen.height;
var W : float = Screen.width;
var Ratio : float = H / W;
Er, the exact same thing will happen in C#, this is nothing to do with Unityscript. Also, it requires knowledge that such a thing as integer division vs. float division even exists.
In C# you would immediately see that they are integers. And who doesn't know about the problems with integer division? You're right, it's as simple as 'promoting' one of them to a float to allow the division to occur properly.
$$anonymous$$any people new to coding don't know that dividing two integers always results in an integer, even if you're putting the results into a float. After all, putting 3 ÷ 2 into a typical calculator results in 1.5 rather than 1. C# code:
var h = Screen.height;
var w = Screen.width;
var ratio = h/w;
print (ratio);
You were saying? But even if you used "int h = Screen.height", that doesn't help if you don't know about integer division.
Yes, it's possible to use non-specific typing in C#. That doesn't make it a good idea (in either language), and at least in C# it's not the default.
"Not the default" doesn't make much sense in this context. It's not really any more or less a "default" in C# than it is in Unityscript. It's a feature which you can use, or not. "Doesn't make it a good idea" is totally a matter of opinion. It wouldn't have ended up in the language if somebody influential didn't think it was a good idea. ($$anonymous$$y opinion is that like all tools it can be abused, but overall it's a good idea, just be aware of what you're doing.) The term is "type inference", by the way.
Answer by Blue-Studios · Aug 02, 2017 at 12:06 PM
Just try... var W : float = Screen.width * 1.0; I tried it and the issue was gone.
The * 1.0
is not required. When you assign an integer to a float variable it is automatically converted to a float. So your answer (five years later) just state the same as the existing answers.
Your answer
Follow this Question
Related Questions
Android screen orientation: why am I getting wrong screen size values? 1 Answer
Screen height and width confusion 1 Answer
Screen.width and Screen.height sometimes flipped for Android 1 Answer
When are Screen.width and Screen.height updated after changing the Screen.orientation value? 0 Answers
Unity 3.5.7 Android Screen returns the wrong value? 0 Answers