PlayerPrefs Unity3D

2

I'm developing an android game but I'm having a hard time saving the input type. For example in this game the user can choose whether to play on the touch or accelerometer, in unity + remote this exchange works well but when I compile and install the apk in the cell phone does not work. Can anyone 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();
    }

}
    
asked by anonymous 07.06.2016 / 15:30

1 answer

1

Hello, It looks like the value variable is not worth PlayerPrefs when you make the condition in Start () . You verify that playerChoice exists in the saved file (PlayerPrefs), but does not load to value . I think that's what's missing in your code. Because Start () runs in the frame where the script is enabled, then the value value would then 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 have helped.

    
08.05.2017 / 23:22