How to create a difficulty slider/setting for a unity game in a setting scene
I am having a hell of a tie creating a difficulty setting. I have tried a slider and a dropdown and neither have worked. I need a system where a user can decide the difficulty in one scene and then load a level and have the difficulty persist. I have tried statics, playerprefs, and instance. Nothing has worked for me and I'm honestly quite frustrated that there is no tutorial on making a difficulty. Now I'm going to attach my code so that maybe you fellas can help me troubleshoot whats going on. Any Help Is greatly appreciated, thanks! O and I have left in pretty much every attempt I have made at this difficulty thing. So it's a touch messy. using UnityEngine; using UnityEngine.UI; using System.Collections;
public class UIManager : MonoBehaviour { public float difficulty; private Slider diffSlider; private Dropdown diffDropDown; private static UIManager _instance; public static UIManager Instance { get { if(_instance==null){ GameObject go = new GameObject("UIManager"); go.AddComponent(); } return _instance; } } void Awake() { diffSlider = gameObject.GetComponent(); _instance = this; }
public string level;
public GameObject HowToPlayText;
private bool showText;
public GUIText howToPlayText;
public float diffSlideAmount;
string difficultyKey = "Difficulty";
void Start()
{
showText = false;
HowToPlayText = GameObject.Find("HowToPlayText");
howToPlayText = GetComponent<GUIText>();
ShowText();
diffDropDown.onValueChanged.AddListener(delegate {
diffDropDownValueChangedHandler(diffDropDown);
});
}
/*public void OnValueChanged(float newValue)
{
newValue = diffSlider.value;
diffSlideAmount = diffSlider.value;
PlayerPrefs.SetFloat(difficultyKey, diffSlideAmount);
PlayerPrefs.Save();
Debug.Log("Difficulty: " + diffSlideAmount);
}*/
// Update is called once per frame
void Update()
{
}
public void OnChoiceChange( )
{
if (diffDropDown.value == 0)
{
difficulty = 0.25f;
}
if (diffDropDown.value == 1)
{
difficulty = .5f;
}
if (diffDropDown.value == 2)
{
difficulty = 1f;
}
if (diffDropDown.value == 3)
{
difficulty = 3f;
}
Debug.Log("Difficulty:" + difficulty);
}
public void ShowText()
{
if(Input.GetButtonDown("How To Play") && showText == false)
{
howToPlayText.enabled = true;
showText = true;
}
if (Input.GetButtonDown("How To Play") && showText == true)
{
howToPlayText.enabled = false;
showText = false;
}
}
void Destroy()
{
diffDropDown.onValueChanged.RemoveAllListeners();
}
public void LoadLevel(string level)
{
Application.LoadLevel(level);
}
private void diffDropDownValueChangedHandler(Dropdown target)
{
Debug.Log("selected: " + target.value);
}
public void SetDropdownIndex(int index)
{
diffDropDown.value = index;
}
}
Answer by KuR5 · Jun 07, 2016 at 07:51 AM
Here we have modified code, take a look.
private void diffDropDownValueChangedHandler(Dropdown target)
{
Debug.Log("selected: " + target.value);
PlayerPrefs.SetInt("difficultyKey", target.value);
OnChoiceChange();
}
void Start()
{
showText = false;
HowToPlayText = GameObject.Find("HowToPlayText");
howToPlayText = GetComponent<GUIText>();
ShowText();
SetDropdownIndex(PlayerPrefs.GetInt("difficultyKey", 0));
diffDropDown.onValueChanged.AddListener(delegate {
diffDropDownValueChangedHandler(diffDropDown);
});
}
public void OnChoiceChange( )
{
.....
.....
Debug.Log("Difficulty:" + difficulty);
// save the difficulty's value here in PlayerPrefs and you can use it in future as well
}
Your answer
Follow this Question
Related Questions
Disable a single Dropdown option. 2 Answers
Slider representing player's movement 1 Answer
How to change the Font Asset from a Dropdown List item? 1 Answer
Adjusting Audio Volume via UI Slider 0 Answers
Held UI button C# 0 Answers