- Home /
How to save my settings
Hi, I made a slider using the Ui and when its move, the directional light within the scene will increase or decrease the intensity. Problem is I have multiple scenes and I dont know how to save my settings. First I used DonDestroyOnLoad, but when I go back to the original scene, the light source will increase by one because theyre never being destroyed. I tried looking into PlayerPres but couldnt seem to get that to work. Any help would be appreciated. Heres the script if your curious.
using UnityEngine; using System.Collections;
public class LightSliderScript : MonoBehaviour {
// Use this for initialization
public float LightAmount = 0;
public Light LightSource;
void Start ()
{
LightSource = GetComponent<Light> ();
//DontDestroyOnLoad(transform.gameObject);
}
void Update ()
{
LightSource.intensity = LightAmount;
PlayerPrefs.SetFloat ("", LightAmount);
}
public void AdjustLight(float NewAmount)
{
LightAmount = NewAmount;
}
}
Answer by _joe_ · Mar 28, 2015 at 04:39 PM
Keep in mind that Start is only called once. Also you should force save the PlayerPrefs using PlayerPrefs.Save() And you need to get the saved data when you start using PlayerPrefs.GetFloat()
Here you go:
using UnityEngine; using System.Collections;
public class LightSliderScript : MonoBehaviour {
// Use this for initialization
public float LightAmount = 0;
public Light LightSource;
void Start ()
{
LightSource = GetComponent<Light> ();
LightSource.intensity = PlayerPrefs.GetFloat("lightIntensity");
DontDestroyOnLoad(transform.gameObject);
}
void Update ()
{
LightSource.intensity = LightAmount;
PlayerPrefs.SetFloat ("lightIntensity", LightAmount);
PlayerPrefs.Save();
}
public void AdjustLight(float NewAmount)
{
LightAmount = NewAmount;
}
}