So in my game you can name your virtual pet. However after I name it and quit the game, the name isn’t remembered when I open the game again as it should. The default name is “chao”. Any help is appreciated.
Game manager code:
public GameObject nameText;
public GameObject namePanel;
public GameObject nameInput;
public GameObject chao;
void Update ()
{
happinessText.GetComponent<Text>().text = "" + chao.GetComponent<Chao>().happiness;
hungerText.GetComponent<Text>().text = "" + chao.GetComponent<Chao>().hunger;
nameText.GetComponent<Text>().text = chao.GetComponent<Chao>().name;
}
public void triggerNamePanel (bool b)
{
namePanel.SetActive(!namePanel.activeInHierarchy);
if (b)
{
chao.GetComponent<Chao>().name = nameInput.GetComponent<InputField>().text;
PlayerPrefs.SetString("name", chao.GetComponent<Chao>().name);
}
}
“Chao” pet code:
[SerializeField]
private string _chaoName;
private bool _serverTime;
void Start ()
{
//PlayerPrefs.DeleteAll();
//PlayerPrefs.SetString ("then", "5/12/2017 01:20:12");
updateStatus();
if (!PlayerPrefs.HasKey("chaoName"))
{
PlayerPrefs.SetString("chaoName", "chao");
_chaoName = PlayerPrefs.GetString("chaoName");
}
}
public string chaoName
{
get { return _chaoName; }
set { _chaoName = value; }
}
public void saveChao ()
{
if (!_serverTime)
updateDevice();
PlayerPrefs.SetInt("_hunger", _hunger);
PlayerPrefs.SetInt("_happiness", _happiness);
}
