- Home /
C# Issues with either PlayerPrefs.SetVariable or UnityEvent.Invoke
I'm having some issues either saving with PlayerPrefs.SetVariable or loading with UnityEvent.Invoke. I'm trying to save both strings and bools in two separate classes. I'm not sure which since If one doesn't work it cancels out the other. I've checked the console and it had no errors logged. As far as I can tell syntax wise both are correct. Is there something else I'm missing?
using UnityEngine;
using UnityEngine.Events;
public class UserSettingString : MonoBehaviour
{
public string stringText { get; set; }
public UserSettingEventString onSettingLoaded;
void OnEnable()
{
Load();
}
public void Load()
{
onSettingLoaded.Invoke(stringText = PlayerPrefs.GetString(name));
}
public void Save()
{
PlayerPrefs.SetString(name, stringText);
}
}
[System.Serializable]
public class UserSettingEventString : UnityEvent<string>
{
}
using UnityEngine;
using UnityEngine.Events;
public class UserSettingBool : MonoBehaviour
{
public bool boolToggle { get; set; }
public UserSettingEventbool onSettingLoaded;
void OnEnable()
{
Load();
}
public void Load()
{
onSettingLoaded.Invoke(boolToggle = PlayerPrefsX.GetBool(name));
}
public void Save()
{
PlayerPrefsX.SetBool(name, boolToggle);
}
}
[System.Serializable]
public class UserSettingEventbool : UnityEvent<bool>
{
}
How do you know it's not working? Have you checked the value of the variables after the assignment?
Also, unless you simply left it out, nothing is subscribing to the events, so your Invoke() is just publishing to thin air. I'm assu$$anonymous$$g you have other code somewhere subscribed to the events?
Actually yes that's exactly what happened. After you mentioned that I double checked and found that I wasn't assigning any of my values.
Answer by MileSplit · Oct 16, 2015 at 10:28 PM
Try calling the PlayerPref.Save() method after setting a player pref.
http://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html