How to keep dropdown values after a scene switch or change them in a script
I'm using 3 scenes a menu scene, a settings scene and a game scene. In my settings scene some of my settings are using dropdowns but when ever I reload the scene it resets, I need it to stay the same, if I could change the values I could just have it change the value every load but I cant find out how to do it, I would greatly appreciate if anyone has a solution.
EDIT: I think some may not understand I need to know how to change the dropdown value, not how to store it. Also I want to change InputField text but I don't know how without it changing back.
Answer by Chethan007 · Jun 20, 2021 at 12:07 PM
@Landern I think this code might help you. I am in a similar situation where the skyBox I am changing in the setting menu. This is the part for the dropdown
using UnityEngine;
using UnityEngine.UI;
public class DropDownSky : MonoBehaviour
{
public Dropdown dropDown;
private void Start()
{
dropDown.value = PlayerPrefs.GetInt("DROPDOWNSKY");
dropDown.onValueChanged.AddListener(delegate { DropdownItemSelected(dropDown); });//dropDown listener
}
public void DropdownItemSelected(Dropdown dropdown)//a delegate , a method which is invoked when the value of the dropdown is changed
{
switch(dropdown.value)
{
case 0: PlayerPrefs.SetInt("DROPDOWNSKY", 0);
break;
case 1: PlayerPrefs.SetInt("DROPDOWNSKY", 1);
break;
case 2:PlayerPrefs.SetInt("DROPDOWNSKY", 2);
break;
case 3:PlayerPrefs.SetInt("DROPDOWNSKY", 3);
break;
case 4:PlayerPrefs.SetInt("DROPDOWNSKY", 4);
break;
case 5: PlayerPrefs.SetInt("DROPDOWNSKY", 5);
break;
}
}
}
Answer by Landern · Oct 21, 2016 at 01:08 PM
Simple solution, use PlayerPrefs to save the DropDown.value for each drop down, retrieve it from PlayerPrefs when you load the screen or need that look up value. The value variable for DropDown controls is the index in the list of the item where 0 is the first item, 1 is the second and so on. Further more if you needed the text the DropDown.value corresponds to the OptionData list from DropDown.options and refers to the index in that list. I've linked all, the documentation that you should need.
I don't think I explained myself, I need to know how to change DropDown.value
@Landern Bro you helped me a lot. So In my settings menu there are 2 drop downs, quality and resolution. So according to your above reply, I can store dropdown values by using Dropdown.value in player prefs . Great. I'll be thankful to you if you send me a sample code. Thank you so much
Your answer
Follow this Question
Related Questions
Movement Unit (RTS) 0 Answers
Car from Unity's standard assets giving me of null reference errors 0 Answers
How do I resolve, get_isActiveAndEnabled can only be called from the main thread error 1 Answer
How do you move the camera with the player. Whats wrong with my code? 0 Answers
Gravity problem 1 Answer