Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Why doesn't my code work? There's no errors, it just doesn't work in game.

Discussion in 'Scripting' started by Chewwyberries21, Jul 14, 2023.

  1. Chewwyberries21

    Chewwyberries21

    Joined:
    Jun 12, 2023
    Posts:
    5
    So, i'm trying to make it so the player can change the resolution of the game ussing the down arrow while in menu, but it's not working.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class changeresolution : MonoBehaviour
    {
    public GameObject defaultres;
    public GameObject two;
    public GameObject fullhd;


    // set default resolution at the start of the game
    void Start()
    {
    defaultres.gameObject.SetActive(true);
    two.gameObject.SetActive(false);
    fullhd.gameObject.SetActive(false);
    Screen.SetResolution(540, 320, true);
    }
    // change resolution
    void Update()
    {
    if (defaultres.activeSelf && Input.GetKeyDown(KeyCode.DownArrow))
    {
    defaultres.gameObject.SetActive(false);
    two.gameObject.SetActive(true);
    Screen.SetResolution(1200, 1800, true);
    }
    if (two.activeSelf && Input.GetKeyDown(KeyCode.DownArrow))
    {
    two.gameObject.SetActive(false);
    fullhd.gameObject.SetActive(true);
    Screen.SetResolution(1920, 1080, true);
    }
    if (fullhd.activeSelf && Input.GetKeyDown(KeyCode.DownArrow))
    {
    fullhd.gameObject.SetActive(false);
    defaultres.gameObject.SetActive(true);
    Screen.SetResolution(540, 320, true);
    }
    }
    }
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    1. Use code tags
    2. Define 'not working'
    3. Did you already check the generated logs?
    4. If so, add logs to the code to see what the values are to troubleshoot
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    You can't change the screen resolution while in the editor. If you do a 'build and run' then it may work.
     
  4. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    483
    You need to look at your logic a bit more.
    Everything runs in the same frame/tick, so all of your if statements are evaluated one after another. So what you have is effectively this:
    - is the defaultres active and did I press the down arrow in this frame/tick? Because you did, then set the defaultres object to be inactive, set the two object to be active, and set the relevant resolution.

    - is the two object active (which it is as it just got set to active in the above if statement), and did I press the down arrow in this frame/tick (which you did as it is still within the same frame/tick as when it checked the previous if statement)? Because these are both true, then set the two object to be inactive, set the fullhd object to be active, and set the relevant resolution.

    - is the fullhd object active (which it is as it just got set to active in the above if statement), and did I press the down arrow in this frame/tick (which you did as it is still within the same frame/tick as when it checked the previous if statement)? Because these are both true, then set the fullhd object to be inactive, set the defaultres object to be active, and set the relevant resolution.

    As you can see, these are evaluated one after another in the same frame/tick, so each separate if statement is overriding the one before it and you just end up back at the defaultres each frame/tick that you press the down arrow.

    You should change the following lines to use else if instead so that only one of the if statements will ever be evaluated as true in a single frame/tick when you press the down arrow:
    if (two.activeSelf && Input.GetKeyDown(KeyCode.DownArrow))

    should be
    else if (two.activeSelf && Input.GetKeyDown(KeyCode.DownArrow))


    and
    if (fullhd.activeSelf && Input.GetKeyDown(KeyCode.DownArrow))

    should be
    else if (fullhd.activeSelf && Input.GetKeyDown(KeyCode.DownArrow))
     
    Bunny83, Ryiah, zulo3d and 1 other person like this.
  5. Chewwyberries21

    Chewwyberries21

    Joined:
    Jun 12, 2023
    Posts:
    5
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,559
    Sounds like you wrote a bug!

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    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 names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

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

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

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

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    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

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  7. Chewwyberries21

    Chewwyberries21

    Joined:
    Jun 12, 2023
    Posts:
    5
    ...i fixed it already
     
  8. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Share the fix for when others have the issue
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Fourth post has the fix. Each of his
    if
    statements were setting up the values for the next one but because they were
    if
    rather than
    else if
    they were executing immediately the same frame. Alternative solutions would have included reversing the order and using a switch statement.

    You could also write the code like this which would allow you to much more easily add new resolutions as well as define them directly in the Inspector. I've included support for moving in both directions. Lines 30 and 35 use the modulo (
    %
    ) sign to loop back around when they reach the end.

    Code (csharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ChangeResolution : MonoBehaviour
    7. {
    8.     [Serializable]
    9.     public struct ResolutionSetting
    10.     {
    11.         public GameObject gameObject;
    12.         public Vector2Int resolution;
    13.     }
    14.  
    15.     public List<ResolutionSetting> resolutionSettings;
    16.     public int defaultResolution;
    17.  
    18.     private GameObject currentlyActiveGameObject;
    19.     private int currentResolution;
    20.  
    21.     void Start()
    22.     {
    23.         SetResolution(defaultResolution);
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         if (Input.GetKeyDown(KeyCode.UpArrow))
    29.         {
    30.             currentResolution = (currentResolution + resolutionSettings.Count - 1) % resolutionSettings.Count;
    31.             SetResolution(currentResolution);
    32.         }
    33.         else if (Input.GetKeyDown(KeyCode.DownArrow))
    34.         {
    35.             currentResolution = (currentResolution + 1) % resolutionSettings.Count;
    36.             SetResolution(currentResolution);
    37.         }
    38.     }
    39.  
    40.     void SetResolution(int number)
    41.     {
    42.         if (currentlyActiveGameObject != null)
    43.         {
    44.             currentlyActiveGameObject.SetActive(false);
    45.         }
    46.  
    47.         var currentSetting = resolutionSettings[number];
    48.         currentlyActiveGameObject = currentSetting.gameObject;
    49.         currentlyActiveGameObject.SetActive(true);
    50.  
    51.         var resolution = currentSetting.resolution;
    52.         Screen.SetResolution(resolution.x, resolution.y, true);
    53.     }
    54. }
     
    Last edited: Jul 15, 2023
    Bunny83 and DevDunk like this.