Problem Retrieving Monitor and Window Width

Hi all~

Once again I am foiled by something simple. I have read what posts I can find about this here and it seems straightforward. Yet when I use the very same calls I get errors.

I want to retrieve the current monitor resolution width, as well as the current screen width.

I have two variables, deskW : float and winW : float in my script attached to an empty GameObject which is the parent to my camera.

This line:

deskW = Resolution.width;

results in the following error:

"An instance of type ‘UnityEngine.Resolution’ is required to access non static member ‘width’.

Whereas this line:

winW = Screen.width;

returns the actual resolution in pixels of the game window, as expected.

What am I missing here? Why does one work but not the other?

Thanks for your reply~!

bOB

Resolution is a struct; Screen is a class. Look up Screen.resolutions in the docs.

–Eric

Hi Eric,

Thanks for the swift reply.

‘Resolution’ is listed in the Runtime Class list, just three items above ‘Screen’.

I found Screen first, and Screen.width worked, so assumed Resolution.width would work the same.

Now I see that it is a struct returned by Screen.resolutions. But Screen.resolutions says it returns all fullscreen resolutions supported by the monitor…

Yet I am looking for the current resolution of the monitor :slight_smile:

Is there a special call for that?

Thanks,

bOB

I guess it should be called “Runtime Classes and Structs” 'cause there are a bunch of those too. :slight_smile:

You can try Screen.currentResolution.

–Eric

I found Screen.currentResolution.

The description says, “if the player is running in window mode, this returns the current resolution of the desktop.”

Awesome!

So now my code reads:

deskW = Screen.currentResolution.width;

But it’s returning a value of 640, and my monitor is set to 1680. I tried scaling the Unity window around and even if my Screen.width climbs above the next highest resolution my monitor supports, deskW still returns 640.

I know I’m missing something here…but what could it be? :?

bOB

I am now also retrieving height. Curiously it’s returning 480. So it’s reporting my desktop at 640 x 480, (4:3), even though it’s set to 1980x1050 (16:10).

That is, of course, the first resolution the monitor is capable of supporting.

Drat. I know I’m gonna kick myself when I learn what the problem is… :roll:

bOB

Thanks for pointing me in the right direction, Eric.

I am still not successfully determining my actual desktop res using Screen.currentResolution.width and Screen.currentResolution.height.

However, you answered my original question, so I’m starting a new thread: “Screen.currentResolution.width returns wrong value?”.

Thanks again~!

bOB