Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gratz · May 08, 2012 at 09:05 PM · screenscreen.widthscreen.height

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gratz · May 08, 2012 at 09:24 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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;
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · May 08, 2012 at 09:14 PM 0
Share

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.

avatar image syclamoth · May 08, 2012 at 09:22 PM 0
Share

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.

avatar image Eric5h5 · May 08, 2012 at 09:36 PM 0
Share

$$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.

avatar image syclamoth · May 08, 2012 at 11:18 PM 0
Share

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.

avatar image Eric5h5 · May 08, 2012 at 11:48 PM 1
Share

"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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 02, 2017 at 02:16 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges