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;
}
}