Question by
ALEGOMan · Apr 16, 2016 at 01:26 AM ·
arrayplayerprefsserializefieldarrayprefs
How do I get a serialized field saved into a PlayerPref?
When my player dies and then respawns his UI status indicator(health bar and text) has been removed from the array. I just learned about playerprefs so I might need to put my status indicator in there.
The code below is on my player... using UnityEngine; using System.Collections;
public class Player: MonoBehaviour {
[System.Serializable]
public class PlayerStats{
public int maxHealth = 100;
private int _curHealth;
public int curHealth
{
get { return _curHealth; }
set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }
}
public void Init()
{
curHealth = maxHealth;
}
}
public PlayerStats stats = new PlayerStats();
public int fallBoundary = -20;
public string deathSoundName = "DeathVoice";
public string damageSoundName = "Grunt";
private AudioManager audioManager;
[SerializeField]
private StatusIndicatorPlayer statusIndictorPlayer;
void Start()
{
stats.Init();
stats.curHealth = PlayerPrefs.GetInt("PlayerCurHealth");
stats.maxHealth = PlayerPrefs.GetInt("PlayerMaxHealth");
if (statusIndictorPlayer == null)
{
Debug.LogError("no staus indicator referenced on player");
}
else
{
statusIndictorPlayer.SetHealth(stats.curHealth, stats.maxHealth);
}
audioManager = AudioManager.instance;
if (audioManager == null)
{
Debug.LogError("oh no, no audioManager in scene");
}
}
void Update(){
if (transform.position.y <= -20) {
DamagePlayer (999999);
}
}
public void DamagePlayer (int damage){
stats.curHealth -= damage;
if (stats.curHealth <= 0)
{
audioManager.PlaySound(deathSoundName);
GameMaster.KillPlayer (this);
}
else
{
audioManager.PlaySound(damageSoundName);
}
statusIndictorPlayer.SetHealth(stats.curHealth, stats.maxHealth);
}
}
I would really appreciate it if you could help me, it's quite a serious bug, thanks.
Comment
It keeps co$$anonymous$$g up the debug.log no status indicator referenced on player when i get hurt by something