Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Screen.resolutions completely bugged my game. Don't use it!

Discussion in 'Scripting' started by TheBestTroll, Jun 26, 2021.

  1. TheBestTroll

    TheBestTroll

    Joined:
    Nov 22, 2019
    Posts:
    6
    Hi! Recently, I've been working in a simple android game. It simply bugged at some point of the development. I spent like THREE WEEKS trying to undertand what was happening, and I wasn't suceeding at all. After debugging a build with Android Studio I FINALLY discovered it was happening due to a DAMN bug in Screen.resolutions.
    I was only trying to get the max frameRate of the device the game is running, so I would be able to limit the fps to the device's limit. SO, TO EVERYONE: DON'T USE SCREEN.RESOLUTIONS IN ANDROID BUILDS: IT WILL PROBABLY BUG, AND WON'T RETURN WHAT YOU'RE LOOKING FOR, CAUSING BUGS AND STRESS.
     
  2. TheBestTroll

    TheBestTroll

    Joined:
    Nov 22, 2019
    Posts:
    6
    Also, it's good to say: everything was completely ok in the editor AND in the PC builds, that only happen in android builds. So yes, that 's a inconsistency between editor and android build. (saying explicitly to link this to searches)
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    That is a lot of words to not actually include any information as to what the bug was. Other than "don't use it" (which is a complete non-starter right off the bat, there is just no other straightforward way to get the information that Screen.resolutions has) it's not possible for us to use the information here in any useful way.

    What were you trying to accomplish with Screen.resolutions? What values did you expect? What values did you get instead? Was it on all Android devices or on particular devices?

    I get that you're pissed at having wasted time due to a bug, we've all been there, but this post accomplishes absolutely nothing. And if you wrote this post instead of reporting the bug to Unity so they can fix it, then you've just wasted time and effort you could have spent actually preventing the bug from affecting other people. (And when you do report the bug to Unity, make sure you include answers to all the questions above, or they won't be able to work on it - or better yet, a small project with which they can easily reproduce the issue and see the problem.)
     
    Bunny83, Munchy2007, Lekret and 3 others like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,005
    oh dear best troll, perhaps you haven't heard of this time-saver:

    Code (csharp):
    1. Debug.Log( "Show some useful Information such as how many resolutions are listed here....");
    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  5. TheBestTroll

    TheBestTroll

    Joined:
    Nov 22, 2019
    Posts:
    6
    Thanks @Kurt-Dekker and @StarManta for answering!
    I undertand you should be thinking I don't actually know what I'm doing, but I'm a little experienced, and for sure, I debugged using logs, and nothing bugged! The bug only happened in android builds.

    I'm sorry, forgot to say what happened, so let's detail a little bit:
    • This might be an old, and already solved issue, because I'm using Unity 5.6.7f1 64bits.
    • I was trying to use Screen.resolutions[0].refreshRate to limit the game's max fps to the device's limit.
    • That worked properly in the editor AND in the PC builds, but when tried to get that value in an Android device, it simply won't return anything, due to an [Array index is out of range] error. I never thought that would be empty, because every device has at least one working resolution: never thought that Screen.resolutions array would have no elements.

    Also, I'm sorry for not being fair, saying "Dont use it!", I know it's maybe a solved issue. Just wanted to warn everyone that may cause bugs, which I don't want to happen with anyone else.
     
  6. StinkySteak

    StinkySteak

    Joined:
    Jul 7, 2017
    Posts:
    19
    I stumbled upon this issue too, It seems only happened only in android 5. Im on unity 2021.3.21f1.
    For some reason, the
    Screen.Resolutions.Length
    is 0
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,100
    So how sure are you about that? Where and when did you read the Screen.resolutions property? As far as I remember you may not get certain information in the very first frame(s).

    The main point of that array is to provide the user a list of resolutions to choose from and this information has to be queried from the system / hardware which may take some time. So instead of spending "weeks", just do some debugging. Try reading it in Awake, try again in Start and try again on a button press at a later time and see what you get back.
     
  8. StinkySteak

    StinkySteak

    Joined:
    Jul 7, 2017
    Posts:
    19
    How am I Sure?
    100% not in the first frame, I only apply the setting in the game scene. The game is fine on the main menu, but when load the game scene and apply the setting, things not doing well. Some developers also has issue on it too such as on iOS.

    What Im so confused is the API is one-threaded then why you said I need to wait some frames?

    Also, Im pretty sure this issue happened on Android 5. The android 5 users were complaining the game stuck and we are getting a lot of negative reviews
     
    Last edited: May 4, 2024
  9. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    550
    I don't know how Screen.resolutions should behave on Android, maybe it's a bug, maybe it's on purpose that they don't return anything on Android. But reading a random (why index 0?) resolution entry to modify the frame rate seems like the wrong solution for the problem anyways:

    Try https://docs.unity3d.com/2023.2/Documentation/ScriptReference/Screen-currentResolution.html to get the resolution information and use https://docs.unity3d.com/ScriptReference/Application-targetFrameRate.html to limit the frame rate.
     
  10. StinkySteak

    StinkySteak

    Joined:
    Jul 7, 2017
    Posts:
    19
    My issue has been solved anyway, its just a bug that appear on a certain android version. I just wanted to get the best resolution which is
    Screen.resolutions[^1]
     
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,448
    I know it's returning the highest resolution but is it returning the highest refresh rate too?
    Code (csharp):
    1. var highestResolution = Screen.resolutions
    2.     .OrderByDescending(r => r.width * r.height)
    3.     .ThenByDescending(r => r.refreshRate)
    4.     .FirstOrDefault();