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 3D-Magic-VR · Jan 18, 2012 at 10:27 PM · nullreferenceexceptionscreengui.boxscreen.widthscreen.height

Screen.currentResolution = Null?

Hi every one, I found this little issue (Unity 3.4.2):

 var showOnOff : boolean;
 var useScreen : boolean;
 //
 function OnGUI {
 //
     if (!showOnOff) {
         if (GUI.Button(Rect(), "Title Screen")) {
             showOnOff = true;
         }
     }
     else {
         GUI.Box(Rect(0, 0, useScreen ? Screen.width : 640, useScreen ? Screen.height : 480), "This is the Title");
     }
 }

When I try to draw a gui box using the screen size the debuger show this:

NullReferenceException: Object reference not set to an instance of an object

Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) Screen.OnGUI () (at Assets/Screen.js:11) This is kind of weird because some time before I can use this type of function in my projects (Unity 3.3), even if I try to check the screen resolution using this: Debug.Log (Screen.currentResolution); The debuger show this:
N

ull

UnityEngine.Debug:Log(Object)

How can this be possible!, any help will be appreciated, thanks in advance.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Jan 18, 2012 at 10:43 PM

Try putting 'new' in front of Rect, and Rect() needs 4 integer numbers listed there, right? I think the null you are seeing is from the Rect, not Screen

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 3D-Magic-VR · Jan 18, 2012 at 10:52 PM 0
Share

Ok, I already try that and the result it's the same, and the - Null UnityEngine.Debug:Log(Object) - it's in the Debug.Log(Screen.currentResolution) line, without the GUI.Box() line, You had trying that?.

avatar image jahroy · Jan 18, 2012 at 11:06 PM 0
Share

The first thing I would do is get rid of that hideous (in my opinion) ? operator.

$$anonymous$$aybe it will work if you write it in a straightforward manner:

 var theRes : Resolution = new Resolution(640, 480);

 if ( useScreen ) {
     theRes = Screen.currentResolution;
 }

 var theRect : Rect = Rect(0, 0, theRes.width, theRes.height);

 GUI.Box(theRect, "$$anonymous$$aybe this will work");


That being said, if your Debug.Log() statement doesn't work then this won't either!

avatar image jahroy · Jan 18, 2012 at 11:07 PM 0
Share

A quick read of the documentation makes me want to ask if your game is running in windowed mode.

avatar image 3D-Magic-VR · Jan 18, 2012 at 11:40 PM 0
Share

I just trying this in the editor, I even check the manual but as before nothing seems to be wrong, even I try to use a simple variable to see the screen width/height and the result it's the same:

var myScreen Vector2 = Vector2(Screen.width, Screen.height);

in the inspector show this:

$$anonymous$$y Screen

X 0 // This should be 1024.

y 0 // This should be 768.

I remember this could be done (Unity 3.3) but now (Unity 3.4.2) can not, why?

avatar image jahroy · Jan 19, 2012 at 12:06 AM 0
Share

I use Screen.currentResolution all the time in Unity 3.4.

That's why I'm asking if you are running in window mode.

The documentation says Screen.currentResoluton returns the current resolution IF the player is running in winodw mode.

This leads me to believe that it will return null (or garbage) if the player is NOT in window mode.

I'm using Unity 3.4.1, but it definitely works for me. I just tested it.

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

6 People are following this question.

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

Related Questions

Screen.width and Screen.height sometimes flipped for Android 1 Answer

How to tell how many lines of text are in my GUI Box when I use wordWrap 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

Make area adapt to screen size 1 Answer


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