- Home /
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:
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:NullReferenceException: Object reference not set to an instance of an object
Null
UnityEngine.Debug:Log(Object)
How can this be possible!, any help will be appreciated, thanks in advance.
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
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?.
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!
A quick read of the documentation makes me want to ask if your game is running in windowed mode.
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?
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
Follow this Question
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