Search Unity

Question I cannot add my resolution dropdown to the slot in my inspector.

Discussion in 'Scripting' started by BenHenriques, Apr 16, 2021.

  1. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Audio;
    5. using UnityEngine.UI;
    6.  
    7. public class SettingsMenu : MonoBehaviour
    8. {
    9.     public AudioMixer audioMixer;
    10.  
    11.     public Dropdown resolutionDropdown;
    12.  
    13.     Resolution[] resolutions;
    14.    
    15.     void Start()
    16.     {
    17.         resolutions = Screen.resolutions;
    18.  
    19.         resolutionDropdown.ClearOptions();
    20.  
    21.         List<string> options = new List<string>();
    22.  
    23.         for (int i = 0; 1 < resolutions.Length; i++)
    24.         {
    25.             string option = resolutions[i].width + "x" + resolutions[i].height;
    26.             options.Add(option);
    27.         }
    28.  
    29.         resolutionDropdown.AddOptions(options);
    30.     }
    31.  
    32.  
    33.     public void SetVolume (float volume)
    34.     {
    35.         audioMixer.SetFloat("Volume", volume);
    36.     }
    37. So up above I have
    38.  
    39.     public void SetQuality (int qualityIndex)
    40.     {
    41.         QualitySettings.SetQualityLevel(qualityIndex);
    42.     }
    43.  
    44.     public void SetFullScreen (bool isFullScreen)
    45.     {
    46.         Screen.fullScreen = isFullScreen;
    47.     }
    48. }
    49.  
    Up above I have: public Dropdown resolutionDropdown; and thats where I would throw in my actual drop down UI but for some reason it won't let me, I am following a tutorial and the person doing it did it without any trouble and I cannot seem to find anything wrong. This is for a options menu BTW, ty in advance!
     
  2. I don't see anything immediately wrong with your class here, are you sure you're trying to drag and drop the game object which has the Dropdown component on it?
     
  3. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    Im pretty sure I am, the dropdown is a text mesh pro dropdown but I wouldn't think that would be an issue. When I created the object I just went to UI then Dropdown-text mesh pro and after I set it up I couldn't drag it into my resolution dropdown.
     
  4. Try to replace this:
    Code (CSharp):
    1. public Dropdown resolutionDropdown;
    with this:
    Code (CSharp):
    1. public TMP_Dropdown resolutionDropdown;
    Also I think you will need at the top of your file the
    Code (CSharp):
    1. using TMPro;
     
    erfanReo likes this.
  5. This works for me:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.Audio;
    4. using TMPro;
    5.  
    6. public class SettingsMenu : MonoBehaviour
    7. {
    8.     public AudioMixer audioMixer;
    9.     public TMP_Dropdown resolutionDropdown;
    10.     Resolution[] resolutions;
    11.  
    12.     void Start()
    13.     {
    14.         resolutions = Screen.resolutions;
    15.         resolutionDropdown.ClearOptions();
    16.         List<string> options = new List<string>();
    17.         for (int i = 0; 1 < resolutions.Length; i++)
    18.         {
    19.             string option = resolutions[i].width + "x" + resolutions[i].height;
    20.             options.Add(option);
    21.         }
    22.         resolutionDropdown.AddOptions(options);
    23.     }
    24.     public void SetVolume (float volume)
    25.     {
    26.         audioMixer.SetFloat("Volume", volume);
    27.     }
    28.     public void SetQuality (int qualityIndex)
    29.     {
    30.         QualitySettings.SetQualityLevel(qualityIndex);
    31.     }
    32.     public void SetFullScreen (bool isFullScreen)
    33.     {
    34.         Screen.fullScreen = isFullScreen;
    35.     }
    36. }
    screenshot1.png
     
  6. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    Got it to work!! TY!!!
     
  7. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    Index was outside the bounds of the array.
    Hmm just got this error after trying to press play.
     
  8. Code (CSharp):
    1. for (int i = 0; 1 < resolutions.Length; i++)
    1 <
    should be
    i <
     
  9. BenHenriques

    BenHenriques

    Joined:
    Apr 6, 2021
    Posts:
    44
    Perfect it worked!! Thanks again!!