Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question PlayerPrefs no longer working?

Discussion in 'Scripting' started by carlosidrovo83, Feb 4, 2023.

  1. carlosidrovo83

    carlosidrovo83

    Joined:
    Jul 4, 2019
    Posts:
    6
    Started a new project a few days ago and PlayerPrefs seems to be broken? Only the first playerpref is stored and the rest are empty.
    To test I created a new empty project and added a gameobject with this script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TestPrefs : MonoBehaviour
    6. {
    7.     string one = "";
    8.     string two = "";
    9.     string three = "";
    10.  
    11.     void Update()
    12.     {
    13.         if(Input.GetKeyUp(KeyCode.S))
    14.             SaveTest();
    15.         if(Input.GetKeyUp(KeyCode.L))
    16.             LoadTest();
    17.     }
    18.  
    19.     void SaveTest()
    20.     {
    21.         one = "1";
    22.         two = "2";
    23.         three = "3";
    24.         PlayerPrefs.SetString("one",one);
    25.         PlayerPrefs.SetString("two",two);
    26.         PlayerPrefs.SetString("three",three);
    27.         PlayerPrefs.Save();
    28.         Debug.Log("Save:"+one+two+three);
    29.         //console output is "Save:123"
    30.     }
    31.  
    32.     void LoadTest()
    33.     {
    34.         one = PlayerPrefs.GetString("one","X");
    35.         two = PlayerPrefs.GetString("two","X");
    36.         three = PlayerPrefs.GetString("three","X");
    37.         Debug.Log("Load:"+one+two+three);
    38.         //console output is "Load:3XX"
    39.     }
    40. }
    Any help would be apreciated
     
  2. carlosidrovo83

    carlosidrovo83

    Joined:
    Jul 4, 2019
    Posts:
    6
    Edit: Error copying code
    Edit2: Test code seems to be working in the new project, just not in my main one.
    I Simplified the code in the main menu of the original project having playerpref issues.
    Can anyone spot if I am doing anything wrong here?

    Code (CSharp):
    1.     [SerializeField] TMP_InputField inputFieldIP;
    2.     [SerializeField] TMP_InputField inputFieldPort;
    3.  
    4.     string saveNameIP = "prevIP";
    5.     string saveNamePort = "prevPort";
    Code (CSharp):
    1.     public void InputField_Update()
    2.     {
    3.         SaveDefaults();
    4.     }
    5.  
    6.     private void SaveDefaults()
    7.     {
    8.         PlayerPrefs.SetString(saveNameIP, inputFieldIP.text);
    9.         PlayerPrefs.SetString(saveNamePort, inputFieldPort.text);
    10.         PlayerPrefs.Save();
    11.     }
    12.  
    13.     private void LoadDefaults()
    14.     {
    15.         inputFieldIP.text = PlayerPrefs.GetString(saveNameIP, "");
    16.         inputFieldPort.text = PlayerPrefs.GetString(saveNamePort, "");
    17.     }
    18.  
    19.     private void Start()
    20.     {
    21.         LoadDefaults();
    22.     }
     
  3. carlosidrovo83

    carlosidrovo83

    Joined:
    Jul 4, 2019
    Posts:
    6
    Nevermind, i just realized what is happening...
    When the app opens an loads the first field, the input field detects an update and saves both values, overwritting the player prefs with empty fields (except the first field which it just loaded).
    Then it procedes to retrieve the second value from player prefs that has just been overwritten with an empty value.
    /facepalm