R
R
RandomProgrammer2021-06-15 10:24:03
C++ / C#
RandomProgrammer, 2021-06-15 10:24:03

PlayerPrefs not working?

Here is how I save:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Menu : MonoBehaviour
{
    void Start()
    {
        if (!PlayerPrefs.HasKey("Sensitivity"))
        {
            PlayerPrefs.SetFloat("Sensitivity", 1f);
        }
        PlayerPrefs.Save();
    }
    public void OnSensChange(float value)
    {
        PlayerPrefs.SetFloat("Sensitivity", value);
        PlayerPrefs.Save();
    }
}


And load (in another scene):
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{

    private float Sensitivity;

    private void Awake()
    {
        Sensitivity = PlayerPrefs.GetFloat("Sensitivity");
        PlayerPrefs.Save();
    }
}

And the value is not saved.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Kachan, 2021-06-24
@MANAB

!PlayerPrefs.HasKey("Sensitivity") - in this case, there may be a jamb, because if you've ever written a value to PlayerPrefs (in a previous run), it will be there. Therefore, some values ​​may not be saved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question