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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

PlayerPrefs not working on WebGL build?

Discussion in 'Editor & General Support' started by CrazYunderscore, Apr 22, 2019.

  1. CrazYunderscore

    CrazYunderscore

    Joined:
    Apr 11, 2019
    Posts:
    6
    Hi, i have problem with PlayerPrefs. I write simple script and create program with 2 buttons and 1 text , and text should be saved after every button click . Okay, on PC build, it's working right, but when i build same program on WebGL, text just not saving (after program restart the variable is default). Is there any solution for this?
    Code (CSharp):
    1. public Text tekst;
    2.     private int TheVariable;
    3.     void Start()
    4.     {
    5.             TheVariable = PlayerPrefs.GetInt("first");
    6.         tekst.text = TheVariable.ToString();
    7.     }
    8.     //Function called from button 1
    9.     public void AddValue()
    10.     {
    11.         TheVariable++;
    12.         PlayerPrefs.SetInt("first", TheVariable);
    13.         tekst.text = TheVariable.ToString();
    14.  
    15.     }
    16.     //Function called from button 2
    17.     public void decraseValue()
    18.     {
    19.         TheVariable--;
    20.         PlayerPrefs.SetInt("first", TheVariable);
    21.         tekst.text = TheVariable.ToString();
    22.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  3. Kacper1263

    Kacper1263

    Joined:
    Apr 7, 2019
    Posts:
    11
    Hi,
    I am a member of this project.

    We do not use PlayerPrefs.Save because it is not required. However, I also tried to make a Save button and call PlayerPrefs.Save but still without success. Everything works as it should in the editor and on the PC.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    TL;DR:
    PlayerPrefs.Save is required for WebGL.

    Well I would look at PlayerPrefs.Save again, because it isn't required because it is automatically called by OnApplicationQuit(). This works well for the editor and standalone, but not WebGL.

    From a Unity dev in another thread:
    OnApplicationQuit is not implemented for WebGL:
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html
     
    Last edited: Apr 22, 2019
    Kacper1263 likes this.
  5. Kacper1263

    Kacper1263

    Joined:
    Apr 7, 2019
    Posts:
    11
    Ok, good to know but I still do not know why even using PlayerPrefs.Save still files were not saved. I tested it on a local machine as well as on hosting.

    I tried to use scripts from the Asset Store (if it was possible I used the Save option) and the situation was identical. The problem only occurred on webGL. Only this time I got the error "NotSupportedException: System.Reflection.Emit.DynamicMethod ::. Ctor" but it was probably caused by the script. When I tried to write by PlayerPrefs.Save, I did not receive any information in the console.
     
  6. roka

    roka

    Joined:
    Sep 12, 2010
    Posts:
    584
  7. Kacper1263

    Kacper1263

    Joined:
    Apr 7, 2019
    Posts:
    11
    OK :( . Thanks for the information
     
  8. Toscan0

    Toscan0

    Joined:
    Oct 9, 2018
    Posts:
    11
    Im working on WebGl with PlayerPrefs and everything is working without PlayerPrefs.save.
    Unity version 2019.3.9f1
     
    vasanrouth and smallbit like this.
  9. Eloren

    Eloren

    Joined:
    Jul 17, 2019
    Posts:
    21
    2021.3.5f1 not working