Slider Difficulty Setting

Hi, I am trying to make a slider that would be set in a settings screen (along with other things, but this is the first setting) and set the difficulty for the whole game. My current issue is having the slider value pulled from one script and then set equal to a float called difficulty in a separate script. The difficulty variable is then used to effectively determine the number of enemies present in game. Any help would be greatly appreciated.

not sure what you mean by this… can you provide your code?

First you should start with changing the value of the slider and set the min and max of the slider. I am assuming you are using the UI slider. I am also assuming that there is only one instance of the difficulty float.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SliderController : MonoBehaviour{
    public Slider mySlider;
    void Awake(){
        mySlider.GetComponent<Slider> ().value = OtherScript.difficulty;
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class OtherScript : MonoBehaviour{
    public static float difficulty = 2;
    public Slider mySlider;
   
    void Update(){
        //Assuming the min value is 1 and the max is 2
        if (mySlider.GetComponent<Slider>().value == 1){
            difficulty = 1;

        }else if (mySlider.GetComponent<Slider>().value == 2){
            difficulty = 2;
        }else if (mySlider.GetComponent<Slider>().value == 3){
            difficulty = 3;
        }
        Debug.Log ("Difficulty: " + difficulty);
    }
}

Put these scripts on whatever, drag the difficulty slider in their respective slots, and set the min and max of the UI slider in its components.

Good start. I would suggest subscribing to the onValueChanged event instead of checking every frame in Update. It doesn’t change that often to justify a every frame check.

And you can set difficulty directly to the value instead of running through a complicated set of if statements. The if statements add literally nothing to your code.

I am just trying to get the concept across without having to take a lot of screenshots of my inspector. There are many ways to improve the efficiency of my scripts. For example if you wanted to use the onValueChanged event then you could do this and attach to your slider:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SliderController : MonoBehaviour{
    private Slider mySlider;
    void Awake(){
        mySlider = this.gameObject.GetComponent<Slider> ();
        mySlider.value = OtherScript.difficulty;
    }
    public void DifficultySlider(){
        if(mySlider.value == 1){
            //Do something
        }
        if(mySlider.value == 2){
            //Do something
        }
        if(mySlider.value == 3){
            //Do something
        }
        Debug.Log ("Difficulty: " + mySlider.value);
    }
}

This could be simplified even more and maybe a bit more modular but it’s a start.

Ok so I’ve tried the OnValueChanged, and it hasn’t worked, here is some of my code

private Slider diffSlider;
string difficultyKey = “Difficulty”;

void Awake()
{
diffSlider = gameObject.GetComponent();
}

public void OnValueChanged(float newValue)
{
newValue = diffSlider.value;
diffSlideAmount = diffSlider.value;
PlayerPrefs.SetFloat(difficultyKey, diffSlideAmount);
PlayerPrefs.Save();
Debug.Log("Difficulty: " + diffSlideAmount);
}

But this doesnt seem to work, and yes I am using UnityEngine.UI. For some reason this just doesn’t fully function.

have you added that function to the slider’s event in the inspector?

2629482--184811--upload_2016-5-10_16-34-54.png

edit: actually looking at your code again I think you’ve overthought it a little. You don’t need to do half of that since the slider is passing it’s value to the function through the parameter

string difficultyKey = "Difficulty";

public void OnValueChanged(float newValue)
{
PlayerPrefs.SetFloat(difficultyKey, newValue);
PlayerPrefs.Save();
Debug.Log("Difficulty: " + newValue);
}

Ok So I tried this and it seemed like it might work, but the value is always zero. In the log it shows each time the value changed and that each time it is set to zero. It seems that the newValue isn’t being set to anything. Do I need to define newValue? or should it work? Any ideas?