- Home /
problem upgrading with playerprefs.
I have a racing game and I'm trying to make upgrades. once you beat a level you haven't beat before, it gives you an upgrade point you can use to increase your speed or handling. the problem is that it gives me a point like normal when I win, but it gets set to zero when I go back to the start menu. keep in mind that both my level scene and my level select scene use the same code I used two codes for this so it might be very complicated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WinLevel : MonoBehaviour {
public Text WinText;
public Button WinButton;
public Text ButtonText;
public string LevelUnlocked = "Level2";
public int LevelToUnlock = 2;
int LevelReached = PlayerPrefs.GetInt("LevelReached");
public int UpgradePoints = PlayerPrefs.GetInt("UpgradePoints");
public Button[] levelButtons;
public Text[] levelTexts;
public Text[] ButtonTexts;
private bool IsFinished = false;
// Use this for initialization
void Start()
{
LevelReached = PlayerPrefs.GetInt("LevelReached");
UpgradePoints = PlayerPrefs.GetInt("UpgradePoints");
Debug.Log("Points: " + UpgradePoints);
for (int i = 0; i < levelButtons.Length; i++)
{
LevelReached = PlayerPrefs.GetInt("LevelReached");
if (i + 1 > LevelReached)
{
levelButtons[i].enabled = false;
levelTexts[i].enabled = false;
ButtonTexts[i].enabled = false;
}
}
Debug.Log("LevelReached is " + LevelReached);
Debug.Log("LevelToUnlock is " + LevelToUnlock);
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("FinishLine"))
{
WinText.enabled = true;
WinButton.enabled = true;
ButtonText.enabled = true;
if ( LevelToUnlock > LevelReached && !IsFinished)
{
PlayerPrefs.SetInt("LevelReached", LevelToUnlock);
Debug.Log("Level Unlocked!");
PlayerPrefs.SetInt("UpgradePoints", UpgradePoints++);
Debug.Log("Points: " + UpgradePoints);
}
IsFinished = true;
}
}
private void Update()
{
}
}
as well as that code, I'm also using this code for using the upgrades
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Upgrades : MonoBehaviour {
public int UpgradePoints = PlayerPrefs.GetInt("UpgradePoints");
public int Speed = PlayerPrefs.GetInt("Speed", -400);
public int Handling = PlayerPrefs.GetInt("Handling");
public int TurnSpeed = PlayerPrefs.GetInt("TurnSpeed");
public int Boosts = PlayerPrefs.GetInt("Boosts");
public Text PointsText;
public AudioSource Sound;
// Use this for initialization
void Start () {
UpgradePoints = PlayerPrefs.GetInt("UpgradePoints");
Speed = PlayerPrefs.GetInt("Speed");
TurnSpeed = PlayerPrefs.GetInt("TurnSpeed");
PointsText.text = "Points: " + UpgradePoints;
Debug.Log("Points: " + UpgradePoints);
}
// Update is called once per frame
void Update () {
UpgradePoints = PlayerPrefs.GetInt("UpgradePoints");
}
public void SpeedUpgrade(int su)
{
if (UpgradePoints > 0)
{
Sound.Play();
PlayerPrefs.SetInt("Speed", Speed - 50);
PlayerPrefs.SetInt("UpgradePoints", UpgradePoints--);
UpgradePoints = UpgradePoints--;
PointsText.text = "Points: " + UpgradePoints;
Speed = PlayerPrefs.GetInt("Speed");
Debug.Log("Speed: " + Speed);
}
}
public void HandlingUpgrade(int hu)
{
if (UpgradePoints > 0)
{
PlayerPrefs.SetInt("UpgradePoints", UpgradePoints--);
PointsText.text = "Points: " + UpgradePoints;
Sound.Play();
}
}
public void TurnSpeedUpgrade(int tu)
{
if (UpgradePoints > 0)
{
PlayerPrefs.SetInt("UpgradePoints", UpgradePoints--);
PointsText.text = "Points: " + UpgradePoints;
PlayerPrefs.SetInt("TurnSpeed", TurnSpeed + 5);
TurnSpeed = PlayerPrefs.GetInt("TurnSpeed");
Sound.Play();
Debug.Log("TurnSpeed: " + TurnSpeed);
}
}
public void BoostUpgrade(int bu)
{
if (UpgradePoints > 0)
{
PlayerPrefs.SetInt("UpgradePoints", UpgradePoints--);
PointsText.text = "Points: " + UpgradePoints;
Sound.Play();
}
}
}
don't mind all that other stuff. I'm just focusing on the playerprefs when I start a scene. what here might cause a playerpref to go back to zero when the scene is loaded?
Your answer
Follow this Question
Related Questions
Problems with pulling a slider value - (Making sensitivity Sliders) 0 Answers
What for are PlayerPrefs with 2 or more Scenes? 1 Answer
I'm trying to set a high score but I can't display it in another scene? 2 Answers
Playerprefs across Scenes 1 Answer
How to pass array variables from one script in one scene to another script in another scene? 2 Answers