Player Prefs Issue
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 :
Firing Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerFire : MonoBehaviour {
public int maxClips;
public int minClips;
public int currentClips;
void Start () {
maxClips = 0;
currentAmmo = PlayerPrefs.GetInt ("currentAmmo");
currentClips = PlayerPrefs.GetInt ("currentClips");
}
void Update (){
PlayerPrefs.SetInt ("currentClips", currentClips);
PlayerPrefs.SetInt ("maxClips", maxClips);
PlayerPrefs.SetInt ("maxAmmo", maxAmmo);
PlayerPrefs.SetInt ("currentAmmo", currentAmmo);
PlayerPrefs.Save ();
if (currentClips >= maxClips) {
currentClips = maxClips;
}
if (currentClips < minClips) {
currentClips = minClips;
}
}
}
Levelling Up Script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerXP : MonoBehaviour {
PlayerFire playerFire;
public int currentLevel;
public int currentXP;
public int maxXP;
public int minXP;
public int xpToLevelUp;
public int [] toLevelUp;
void Start () {
currentXP = PlayerPrefs.GetInt ("playerXP");
minXP = 0;
playerFire = GetComponent<PlayerFire> ();
}
}
void Update (){
PlayerPrefs.SetInt ("playerXP", currentXP);
PlayerPrefs.SetInt ("maxXP", maxXP = 900);
xpToLevelUp = toLevelUp[currentLevel];
if (currentXP >= xpToLevelUp) {
LevellingUp ();
}
LevellingUp ();
}
public void LevellingUp(){
if (currentXP >= toLevelUp [currentLevel]) {
StartCoroutine (upgradingPlayer ());
currentLevel++;
playerFire.maxClips = playerFire.maxClips + 1;
playerFire.maxAmmo = playerFire.maxAmmo + 10; }
}
}
Home screen script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class EnterItemCollectLevel : MonoBehaviour {
public int maxAmmo;
public int currentAmmo;
public int maxClips;
public int currentClips;
void Start (){
currentAmmo = PlayerPrefs.GetInt ("currentAmmo");
maxAmmo = PlayerPrefs.GetInt ("maxAmmo");
currentClips = PlayerPrefs.GetInt("currentClips");
maxClips = PlayerPrefs.GetInt ("maxClips");
}
void Update ()
{
if (Input.GetKey (KeyCode.I)) {
SceneManager.LoadScene (2);
}
}
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612150709im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
PlayerPref doesnt work 2 Answers
Unlocked levels Lock if a previous level played 0 Answers
Level Unlock System....frustrated,Building a level unlock system using player prefs 0 Answers
PlayerPrefs Not Saving When I Click the Button. 1 Answer
PlayerPrefs works fine in Unity Editor but not on Android 0 Answers