0

I want to save values of some variables. I tried to save it to file, but it doesn't work. So now I try it in another way. But it don't save data too... If I start simulation i Unity, change values of variables, stop simulation and then start it again, I have old values of the variables..

    void OnEnable()
    {        
        LoadFromFile();
    }

    void OnDisable()
    {        
        SaveToFile();
    }

    public void SaveToFile()
    {
        GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
        GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();
        IndicatorsInfo indicatorsInfo = new IndicatorsInfo();

        PlayerPrefs.SetFloat("my_setting", indicatorsInfo.walkSpeedTemfFile);
    }

    public void LoadFromFile()
    {
        GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
        GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();

        gameManager.walkSpeedTemp = PlayerPrefs.GetFloat("my_setting");
    }

    [Serializable]
    class IndicatorsInfo
    {
        public float walkSpeedTemfFile;
    }
Community
  • 1
  • 1
Amazing User
  • 3,473
  • 10
  • 36
  • 75

1 Answers1

2
IndicatorsInfo indicatorsInfo = new IndicatorsInfo();
PlayerPrefs.SetFloat("my_setting", indicatorsInfo.walkSpeedTemfFile);

You are creating a new instance of IndicatorsInfo when you use the new keyword. You saved indicatorsInfo.walkSpeedTemfFile from that new instance without assigning a value to the new indicatorsInfo you created.

Maybe what you are trying to do is save the value from the Editor?

GameObject gameManagerObject = GameObject.FindGameObjectWithTag("GAMEMANAGER");
GAMEMANAGER gameManager = gameManagerObject.GetComponent<GAMEMANAGER>();
PlayerPrefs.SetFloat("my_setting", gameManager.walkSpeedTemp);
Programmer
  • 121,791
  • 22
  • 236
  • 328