3
\$\begingroup\$

This is my current code, it only work into unity, when i test it in an Android Device it don't save the variable

public class Click : MonoBehaviour{

public UnityEngine.UI.Text proiettiliTotali;
public UnityEngine.UI.Text bpc;
public float proiettili = 0.00f;

void OnEnable(){
        proiettili = PlayerPrefs.GetFloat("Player Score");
}

// Update is called once per frame
void Update () {
    proiettiliTotali.text = ConvertiroreProiettili.Instance.GetCurrencyInString(proiettili, false, false);
    bpc.text = ConvertiroreProiettili.Instance.GetCurrencyInString (proiettiliOgniClick, false, true);
}

public void Clicked () {
    proiettili += proiettiliOgniClick;
}

void OnDisable(){

    PlayerPrefs.SetFloat("Player Score", proiettili);
    PlayerPrefs.Save ();
    Debug.Log (proiettili);
}
\$\endgroup\$
  • 3
    \$\begingroup\$ One caution is that PlayerPrefs storage is not secure - the file can be viewed and modified as plaintext by an outside program. The Unity team advises against using this method to store information someone might want to hack, like scores, unlock progress, or in-game currencies. It's intended for preference information, like controls & sound/music settings, things the player can freely modify in-game anyway. \$\endgroup\$ – DMGregory May 4 '15 at 21:22
  • \$\begingroup\$ There are other methods of saving data you can look at. There is a paid course you can get for FREE on Udemy. It covers 5 different methods to save and load data in Unity3D. It covers PlayerPrefs, Text files, Binary Serialization, Xml Serialization and even SQLite! The free coupon link for this course is udemy.com/saving-and-loading-game-data-in-unity3d/… Goodluck! \$\endgroup\$ – Mike Hunt Apr 11 '16 at 11:50
1
\$\begingroup\$

PlayerPrefs creates on file on the disk of the device the app is used on, so it will not save between editor, and device. Other then that your code seems fine, I have noticed that if you build to your device on each test, that sometimes you lose your PlayerPrefs.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.