PlayerPrefs Not Consistently Saving Stats
Dear Coders I'm having a lot of trouble with this. I have created the following scripts : 1) A script that saves stats like health, ammo, clips in a home screen 2) An in-game script in which a player can collect game objects to boost ammo and clips 3) An XP script for the player to level up My scripts generally work perfectly for increasing maximum ammo and maximum clips. These save between my two scenes. I can also increase my current ammo and current clips but this is where the issue starts. For some reason, whilst in game health, ammo and clips increase until their max values have been reached. These figures save in the home screen. But when going back into the game scene, ammo and clips values reduce. Strangely this drop in values in current ammo and current clips seems to correlate with the increases as the player is levelling up. So, for example if maxClips = maxClips + 2, maxClips will ALWAYS increase by 2 on levelling up and I can collect currentClips to the defined maxClips value in the game at that time, save this value into the home screen but then going back into the game scene it will always drop to 4. If maxClips = maxClips + 3, my currentClips will only go up to 6 if I exit out of the game scene and then enter back in and so forth. I'm having exactly the same issue with current health/max health and current ammo/ max ammo. Scripts are as follows :
1. using System.Collections;
2. using System.Collections.Generic;
3. using UnityEngine;
4. using UnityEngine.AI;
5.
6. public class PlayerFire : MonoBehaviour {
7.
8. public int maxClips;
9. public int minClips;
10. public int currentClips;
11.
12.
13. void Start () {
14.
15. maxClips = 0;
16.
17. currentAmmo = PlayerPrefs.GetInt ("currentAmmo");
18. currentClips = PlayerPrefs.GetInt ("currentClips");
19. }
20.
21. void Update (){
22.
23. PlayerPrefs.SetInt ("currentClips", currentClips);
24. PlayerPrefs.SetInt ("maxClips", maxClips);
25.
26. PlayerPrefs.SetInt ("maxAmmo", maxAmmo);
27. PlayerPrefs.SetInt ("currentAmmo", currentAmmo);
28.
29. PlayerPrefs.Save ();
30.
31. if (currentClips >= maxClips) {
32. currentClips = maxClips;
33. }
34.
35. if (currentClips < minClips) {
36. currentClips = minClips;
37. }
38. }
39. }
1. using System.Collections;
2. using System.Collections.Generic;
3. using UnityEngine;
4. using UnityEngine.UI;
5.
6. public class PlayerXP : MonoBehaviour {
7. PlayerFire playerFire;
8.
9. public int currentLevel;
10. public int currentXP;
11. public int maxXP;
12. public int minXP;
13. public int xpToLevelUp;
14. public int [] toLevelUp;
15.
16. void Start () {
17.
18. currentXP = PlayerPrefs.GetInt ("playerXP");
19.
20. minXP = 0;
21.
22. playerFire = GetComponent<PlayerFire> ();
23.
24. }
25. }
26.
27. void Update (){
28.
29. PlayerPrefs.SetInt ("playerXP", currentXP);
30. PlayerPrefs.SetInt ("maxXP", maxXP = 900);
31.
32. xpToLevelUp = toLevelUp[currentLevel];
33. if (currentXP >= xpToLevelUp) {
34. LevellingUp ();
35. }
36.
37. LevellingUp ();
38. }
39.
40. public void LevellingUp(){
41.
42. if (currentXP >= toLevelUp [currentLevel]) {
43.
44. StartCoroutine (upgradingPlayer ());
45.
46. currentLevel++;
47.
48. playerFire.maxClips = playerFire.maxClips + 1;
49. playerFire.maxAmmo = playerFire.maxAmmo + 10; }
50. }
51. }
1. using System.Collections;
2. using System.Collections.Generic;
3. using UnityEngine;
4. using UnityEngine.UI;
5. using UnityEngine.SceneManagement;
6.
7. public class EnterItemCollectLevel : MonoBehaviour {
8. public int maxAmmo;
9. public int currentAmmo;
10. public int maxClips;
11. public int currentClips;
12.
13.
14. void Start (){
15.
16. currentAmmo = PlayerPrefs.GetInt ("currentAmmo");
17. maxAmmo = PlayerPrefs.GetInt ("maxAmmo");
18.
19. currentClips = PlayerPrefs.GetInt("currentClips");
20. maxClips = PlayerPrefs.GetInt ("maxClips");
21. }
22.
23. void Update ()
24. {
25. if (Input.GetKey (KeyCode.I)) {
26. SceneManager.LoadScene (2);
27. }
28. }
In order these are : 1. Firing Script 2. Levelling Up Script 3. Home Screen Script
I appreciate that this may be slightly confusing but any help that anyone could offer would be very much appreciated - I'm tearing my hair out here! Thankyou in advance!