Graphics quality dropdown saves playerpref but displays default?
Hello. I don't write code, but I got help to get this far. I probably just need one or two lines of code to finish this if you don't mind. I created a TextMeshPro dropdown on the Settings Menu canvas to allow users to select Quality setting. I thought it wasn't working, because testing in Unity, if I went from Menu scene to Game Play scene and back, the dropdown text and dropdown value in the Inspector had reset to the default. But a PC build showed me otherwise based on the screen going black if I chose any setting except the most recent. So, it seems that all is working and saving fine except what is being displayed in the dropdown if I leave the Menu scene and return. The setting is saving fine after quitting the game and restarting as well. What exactly do I need to add to this code to make the dropdown not display the default after leaving the Menu scene? Here are the two scripts handling this. The first is attached to the dropdown itself. The second is attached to a canvas in the opening splash scene that lasts only a few seconds. Thank you! using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SettingsMenu : MonoBehaviour
{
public void SetQuality (int qualityIndex)
{
QualitySettings.SetQualityLevel(qualityIndex);
PlayerPrefs.SetInt("_qualityIndex", qualityIndex);
}
}
Here's the second script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GraphicsQualitySave : MonoBehaviour
{
void Start()
{
LoadQuality();
}
private void LoadQuality()
{
if (PlayerPrefs.HasKey("_qualityIndex"))
{
int qualityIndex = PlayerPrefs.GetInt("_qualityIndex");
QualitySettings.SetQualityLevel(qualityIndex);
}
}
}
Answer by Larry-Dietz · Dec 07, 2019 at 03:54 AM
In your menu screen, in a Start method, you need to read the value from PlayerPrefs, the same as you are in that 2nd script, then set the dropdown to display whichever entry is for the quality you got from PlayerPrefs.
To do this, your SettingsMenu class will need a reference to the dropdown.
Try changing your script to this...
public class SettingsMenu : MonoBehaviour
{
public TMPro.TMP_Dropdown QualityDropDown;
void Start()
{
int Quality=PlayerPrefs.GetInt("_qualityIndex", 0);
QualityDropDown.value = Quality;
}
public void SetQuality (int qualityIndex)
{
QualitySettings.SetQualityLevel(qualityIndex);
PlayerPrefs.SetInt("_qualityIndex", qualityIndex);
}
}
Presuming that the quality index you are saving is the actual index of the item in the dropdown, then I believe this should work.
Just make sure you drag the DropDown into the field in the inspector.
Hope this helps, -Larry
Answer by jcam828 · Dec 07, 2019 at 04:21 AM
Thank you so much, Larry. It isn't working yet. I changed the script and saved it, but please excuse my ignorance. I don't know what you mean by dragging the DropDown into the field in the inspector. I don't see a field to drag it to. I tried dragging it into the Inspector field for On Value Changed (Int32), but that didn't work because the actual quality didn't change at all when I went through playing in Unity. Unless I'm choosing the wrong function. I chose "value" at the top. So I put the Settings Canvas back in that field rather than the dropdown. What am I missing?
To clarify, the Settings$$anonymous$$enu script was originally a component of the Settings Canvas. I just experimented and realize that copying that script to the T$$anonymous$$P dropdown object works too. So I can keep it there, but it does the same as before---saving quality setting but resetting to default dropdown value.
Edit: I also get the following error in the console when I press play and click button to switch from $$anonymous$$ain $$anonymous$$enu canvas to the Settings $$anonymous$$enu canvas: NullReferenceException: Object reference not set to an instance of an object Settings$$anonymous$$enu.Start () (at Assets/Scripts/Settings$$anonymous$$enu.cs:12)
Whichever object this script is attached to, when selected in the scene view, should show this script as a component in the inspector. In that component, you should see a field named QualityDropDown, and it should currently be set to None. While this is visible to you, click on the actual dropdown object in scene view, and drag in to the component, replacing the None, with the actual dropdown object.
Once you have the component assigned, as above, your null reference error should go away as well.
That did it! Thank you so much for being here and so quickly responding with exactly what I needed, Larry! Take care.
You are very welcome. Glad I was able to help.
Your answer
Follow this Question
Related Questions
How to display text from a text object to another text object? 1 Answer
Canvas behaves differently on PC fullscreen and WebGL fullscreen 0 Answers
How to fit content into a specified area of screen at game startup 1 Answer
Reserve ammo not displaying on UI 0 Answers
Re-order the items of dropdown list using DragHandler 0 Answers