retrieving a serialized Playerprefs specific value in Unity

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



retrieving a serialized Playerprefs specific value in Unity



I want to retrieve my integer value in my SaveState class which is already serialized as a string in Playerprefs and I want to access its values on my PlayerHealth and store it on a float variable. I'm just new in c# thanks for understanding I hope someone can help me in this.


SaveState


Playerprefs


PlayerHealth



This is my SaveState class:


SaveState


public class SaveState
public float FullHealth = 10; //The value that i Want
public float staminaPTS;



This is my PlayerHealth class:


PlayerHealth


public class PlayerHealth : MonoBehaviour
public SaveState stateREF;
float bulan_fullhp;
float bulan_fullsp;
float currHP;
float currSP;

//HUD Variables
public Slider healthSlider;

void Start ()
load ();
bulan_fullhp = // THIS IS WHERE I WANT TO REFERENCE THE SERIALIZED PLAYERPREF "FUllHealth" VALUE IN MY Save_State Class
currHP = bulan_fullhp;
currSP = bulan_fullsp;

//Hud Iniitialization
healthSlider.maxValue = bulan_fullhp;
healthSlider.value = bulan_fullhp;
Debug.Log (currHP);


public void save()
PlayerPrefs.SetString ("save", Helper.Serialize <SaveState> (stateREF));


public void load()
if (PlayerPrefs.HasKey ("save"))
stateREF = Helper.Deserialize<SaveState> (PlayerPrefs.GetString ("save"));
else

stateREF = new SaveState ();
save ();



public void addDamage(float damage)
if (damage <= 0)
return;
currHP -= damage;
healthSlider.value = currHP;
if (currHP <= 0)
makeDead ();



void makeDead()
Destroy (gameObject);




Here is my Serialization class:


Serialization


public static class Helper

//Serialze
public static string Serialize <T> (this T toSerialize)
XmlSerializer xml = new XmlSerializer (typeof(T));
StringWriter writer = new StringWriter ();
xml.Serialize (writer, toSerialize);
return writer.ToString ();


public static T Deserialize<T> (this string toDeserialze)
XmlSerializer xml = new XmlSerializer (typeof(T));
StringReader reader = new StringReader (toDeserialze);
return (T)xml.Deserialize (reader);






You mention int in the question body, but the code contains floats. Which is it?
– Paul-Jan
Aug 10 at 14:46



int


floats





sorry for that, its a float , I already edited it thanks.
– Ry Ren
Aug 10 at 14:52




2 Answers
2



You going with XML and the process will be similar but I will answer with JSON.



First you have your SaveState as Serializable


[System.Serializable]
public class SaveState

public float FullHealth = 10; //The value that i Want
public float staminaPTS;



At that point you can turn that into JSON with JsonUtility:


SaveState ss = new SaveState();
ss.FullHealth = 10f;
ss.staminaPTS = 20f;
string json = JsonUtility.ToJson(ss);
PlayerPrefs.SetString("Save", json);



next you want to retrieve:


string json = PlayerPrefs.GetString("Save", null);
if(string.IsNullOrEmpty(json) == true) return; // no saving
SaveState ss = JsonUtility.FromJson<SaveState>(json);
Debug.Log($"ss.FullHealth ss.staminaPTS");



simple and does not require external libraries as JsonUtility is part of UnityEngine.



I'm pretty sure I'm not understanding your question, but will


bulan_fullhp = stateRef.FullHealth;



not do exactly what you want in this case?





So basically I want to pass the FullHealth value to my float value in "bulan_fullhp". It just say NullReferenceException Object Reference not set to an Object when I write that code sir.
– Ry Ren
Aug 10 at 14:54






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard