Problem with resolutions

Hi, I am making a resolution option, but I can’t put the dropdownon the resolution dropdown. This is my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class TheOptions : MonoBehaviour
{
    public AudioMixer audioMixer;
    public Dropdown resolutionDropdown;

    Resolution[] resolutions;

    void start()
    {
        resolutions = Screen.resolutions;
        resolutionDropdown.ClearOptions();

        List<string> options = new List<string>();

        int currentResolutionIndex = 0;
        for (int i = 0; i < resolutions.Length; i++)
        {
            string option = resolutions[i].width + " x " + resolutions[i].height;
            options.Add(option);

            if (resolutions[i].width == Screen.currentResolution.width && resolutions[i].height == Screen.currentResolution.height)
            {
                currentResolutionIndex = i;
            }

        }

        resolutionDropdown.AddOptions(options);
        resolutionDropdown.value = currentResolutionIndex;
        resolutionDropdown.RefreshShownValue();
    }

    public void SetResolution(int resolutionIndex)
    {
        Resolution resolution = resolutions[resolutionIndex];
        Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
    }

    public void SetVolume(float volume)
    {
        audioMixer.SetFloat("volume", volume);
    }

    public void SetQuality(int qualityIndex)
    {
        QualitySettings.SetQualityLevel(qualityIndex);
    }
}

6545896--740413--Sin título.png

Why not? What happens when you try? Is this script on a prefab and the Dropdown is in a Scene?

The script is in a prefab nd the dropdown is in the scene. When I try to put the dropdown, it just don’t let me put it there and my mouse is a circle with a / and when I play and click and option on the dropdown it says "
NullReferenceException: Object reference not set to an instance of an object
TheOptions.SetResolution (System.Int32 resolutionIndex) (at Assets/Scripts/TheOptions.cs:41)"

You can’t reference objects from a scene in a prefab. It wouldn’t make sense. What would happen if you instantiated that prefab in a different scene where the object doesn’t exist? If possible, either add your prefab to the scene at edit time, or add the dropdown to the prefab.

If neither of those options works for your game, you need to get a reference to your dropdown at runtime after your prefab is instantiated. Look into FindGameObjectWithTag or other options. Here’s a good article that talks about some options (written by our very own @StarManta ): Regarding GameObject.Find · UnityTipsRedux

I mean, the dropdown is on the scene and the script TheOptions is on an object called OptionsMenu. I tried what you said and it doesn’t work.

Requests to help debug your program should include:

  • Your source code
  • A description of what you were hoping to accomplish
  • A summary of your strategy for making that happen
  • A description of what actually happened instead (emphasizing how it was different from your goal). “It didn’t work” is NOT acceptable as “a description of what actually happened instead”.
1 Like

When I use what Praetor Blue said, unity give an error that sais Assets\Scripts\TheOptions.cs(16,41): error CS0308: The non-generic method ‘GameObject.Find(string)’ cannot be used with type arguments

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class TheOptions : MonoBehaviour
{
    public AudioMixer audioMixer;
    public Dropdown resolutionDropdown;

    Resolution[] resolutions;

    void start()
    {
        resolutionDropdown = GameObject.Find<Resolution>();
        resolutions = Screen.resolutions;
        resolutionDropdown.ClearOptions();

        List<string> options = new List<string>();

        int currentResolutionIndex = 0;
        for (int i = 0; i < resolutions.Length; i++)
        {
            string option = resolutions[i].width + " x " + resolutions[i].height;
            options.Add(option);

            if (resolutions[i].width == Screen.currentResolution.width && resolutions[i].height == Screen.currentResolution.height)
            {
                currentResolutionIndex = i;
            }

        }

        resolutionDropdown.AddOptions(options);
        resolutionDropdown.value = currentResolutionIndex;
        resolutionDropdown.RefreshShownValue();
    }

    public void SetResolution(int resolutionIndex)
    {
        Resolution resolution = resolutions[resolutionIndex];
        Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
    }

GameObject.Find<Resolution>() is wrong. It should be GameObject.Find("Resolution").

Now it says Assets\Scripts\TheOptions.cs(16,30): error CS0029: Cannot implicitly convert type ‘UnityEngine.GameObject’ to ‘UnityEngine.UI.Dropdown’. What I say is, that with my code, I can’t drag my Resolution dropdown to the resolutionDropdown, as you can see on the image at the start.

Were you trying to find by name or by type? If you’re trying to find by type, then you want GameObject.FindObjectOfType. If you’re looking for an object named “Resolution”, then Mauri’s suggestion was correct.

Sounds like you forgot to say “.GetComponent()” somewhere.

Now it says Assets\Scripts\TheOptions.cs(16,31): error CS0246: The type or namespace name ‘ResolutionDropdown’ could not be found (are you missing a using directive or an assembly reference?)

public Dropdown resolutionDropdown = (ResolutionDropdown)FindObjectOfType(typeof(Dropdown));

Do you have a class named ResolutionDropdown? Even if you do, why are you casting to that type when your variable is of type Dropdown?

Also, you probably want to move the initialization of that variable to Awake. I suspect you’ll have problems trying to use Find (any version) in a class variable declaration.

With my code, I can not pur my Dropdown intot the resolutionDropdown, as you can see on the image on the start.

That does not make sense as a response to anything that I said. I am not telling you that you should use the inspector instead of code; I am telling you how to modify your code to fix your error. Your cast is wrong, and your code is (I’m about 90% sure) in the wrong place.

I’m guessing that you found an example somewhere, and you tried to copy it and modify it to your situation, but you don’t actually understand what all of those things mean? In computer programming, it is usually a good idea to stop and figure out what all the different parts mean (or at least get to the point where you can make a confident guess). The typical rule for computer programs is that there are a thousand tiny details, and all of them are easy, but you need to get every single one of them perfectly right or your program will not work. If you got 95% of the details right, the computer will NOT figure out what you were aiming for, it will just fail.

1 Like

I found the error, on the script I was saying that it was a normal dropdown, but my dropdown was a text mesh pro dropdown, but thank you for the help.

If you want to use the TextMeshPro dropdown, use public TMP_Dropdown resolutionDropdown; instead of public Dropdown resolutionDropdown;.

You will need to add using TMPro; on top of your script, though.

I also agree with what @Antistone said. If you don’t really know how to code yet, check out Unity Learn.
It has C# tutorials: Unity C# Survival Guide - Unity Learn