Virtual Pet: Name doesn't store when I quit game and start it again

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);
    }

You are setting the string if it doesn’t exist but aren’t getting it if it does.

Where at?

Here;

 if (!PlayerPrefs.HasKey("chaoName"))
        {
            PlayerPrefs.SetString("chaoName", "chao");
            _chaoName = PlayerPrefs.GetString("chaoName");
        }

I tried this but nothing changed

if (!PlayerPrefs.HasKey("chaoName"))
        {
            PlayerPrefs.SetString("chaoName", "chao");
        }
        if (!PlayerPrefs.HasKey("chaoName"))
        {
            _chaoName = PlayerPrefs.GetString("chaoName");
        }

Oops

if (!PlayerPrefs.HasKey("chaoName"))
        {
            PlayerPrefs.SetString("chaoName", "chao");
        }
        if (PlayerPrefs.HasKey("chaoName"))
        {
            _chaoName = PlayerPrefs.GetString("chaoName");
        }

You probably forgot to save the player prefs after you set it the first time.

How would I go about doing that? Sorry for all the questions, you’re a tremendous help.

The docs have the answers :wink: Unity - Scripting API: