Playerprefs Unity3d

Asked

Viewed 306 times

2

I’m developing a game for android but I’m having a hard time saving the type of input. For example in this game the user can choose whether to play on touch or accelerometer, in Unity + remote works well this exchange but when I compile and install the apk on mobile does not work. Can someone help me?

    void Start()
    {

        if (PlayerPrefs.HasKey("playerChoice"))
        {

            if (value == 1)
            {
                inputAC.SetActive(true);
                inputTC.SetActive(false);
            }

            if (value == 0)
            {
                inputAC.SetActive(false);
                inputTC.SetActive(true);
            }
        }
    }

    void Update()
    {
        if (gameRunning)
            uiManagerScript.ShowScoreText(playerScript.score);


    }


    public void GameOver(int score)
    {
        scoreManger.AddScore(score);
        gameRunning = false;
        playerScript.gameObject.SetActive(false);
        uiManagerScript.GameEnd();
        plataformSpawnerScript.enabled = false;
        playerScript.isDead = true;
        plataform.gameObject.SetActive(false);
    }


    public void StartGame()
    {

        plataformSpawnerScript.enabled = true;
        cameraScript.ResetCam();
        uiManagerScript.GameStart();
        playerScript.score = 0;
        playerScript.isDead = false;
        gameRunning = true;
        plataform.gameObject.SetActive(true);

    }


    public void InputChoiceFalse()
    {
        if (PlayerPrefs.HasKey("playerChoice"))
        {

            PlayerPrefs.SetInt("playerChoice", playerChoice ? 0:1);

            value = 1;
            PlayerPrefs.Save();
        }

    }


    public void InputChoiceTrue()
    {
        if (PlayerPrefs.HasKey("playerChoice"))
        {

            PlayerPrefs.SetInt("playerChoice", playerChoice ? 0 : 1);

            value = 0;
            PlayerPrefs.Save();
        }


    }


    public void QuitGame()
    {
        Application.Quit();
    }

}

1 answer

1

Hello, It seems that the variable value does not have the value of Playerprefs when you make the condition on Start(). You check if it exists playerChoice in the saved file(Playerprefs), but does not load to the value. I think that’s what’s missing from your code. Because the Start() is executed in the frame in which the script is enabled, the value of value then, it would be the previously established value, possibly in the Inspector.

EDIT: I would do so:

...
if (PlayerPrefs.HasKey("playerChoice"))
   {
   value = PlayerPrefs.GetInt("playerChoice");

       if (value == 1)
       {
...

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.