Hey everyone.
I am currently developing a 2D Top Down Racing game and at the moment I am making an upgrade system.
Now unfortunately I have come across an issue which is preventing me from continuing developing my game.
I have a public variable called TopSpeedLevelBought and when the button is pressed (to purchase an upgrade) it is not updated in the inspector and when I debug log it is sends 2 different numbers, the default number which is 0 and the upgrade level which was purchased.
My scripts are a little messy and not the optimal way to do things (Such as the PurchaseUpgrade bit) but later I will go and tidy this up.
My Upgrades script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Upgrades : MonoBehaviour {
public int isNew;
public int SpendableXP;
public int TopSpeedLevelBought;
public int SteeringLevelBought;
private int TopSpeedLevelToBuy;
private int SteeringLevelToBuy;
private UnityEngine.UI.Text TopSpeedText;
private UnityEngine.UI.Text SteeringText;
private float PlayerOneCarAcceleration;
private float PlayerTwoCarAcceleration;
private float StartSpeed = 0.3f;
private float TopSpeedLevelOneSpeed = 0.31f;
private float TopSpeedLevelTwoSpeed = 0.32f;
private float TopSpeedLevelThreeSpeed = 0.33f;
private float TopSpeedLevelFourSpeed = 0.34f;
private float TopSpeedLevelFiveSpeed = 0.35f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
TopSpeedLevelToBuy = TopSpeedLevelBought + 1;
SteeringLevelToBuy = SteeringLevelBought + 1;
SpendableXP = GameObject.Find ("_GameManager").GetComponent<XPandLevels> ().CurrentXpToSpend;
Debug.Log (TopSpeedLevelBought);
if (Application.loadedLevel == 5)
{
if (TopSpeedLevelBought == 0)
{
GameObject.Find ("RaceCar1").GetComponent<CarMovement>().acceleration = StartSpeed;
}
if (TopSpeedLevelBought == 1)
{
GameObject.Find ("RaceCar1").GetComponent<CarMovement>().acceleration = TopSpeedLevelOneSpeed;
}
if (TopSpeedLevelBought == 2)
{
GameObject.Find ("RaceCar1").GetComponent<CarMovement>().acceleration = TopSpeedLevelTwoSpeed;
}
if (TopSpeedLevelBought == 3)
{
GameObject.Find ("RaceCar1").GetComponent<CarMovement>().acceleration = TopSpeedLevelThreeSpeed;
}
if (TopSpeedLevelBought == 4)
{
GameObject.Find ("RaceCar1").GetComponent<CarMovement>().acceleration = TopSpeedLevelFourSpeed;
}
if (TopSpeedLevelBought == 5)
{
GameObject.Find ("RaceCar1").GetComponent<CarMovement>().acceleration = TopSpeedLevelFiveSpeed;
}
Debug.Log (GameObject.Find ("RaceCar1").GetComponent<CarMovement>().acceleration);
}
if (Application.loadedLevel == 8)
{
TopSpeedText = GameObject.Find("TopSpeedUpgrade").GetComponent<Text>();
SteeringText = GameObject.Find("SteeringUpgrade").GetComponent<Text>();
}
if (Application.loadedLevel == 8)
{
if (TopSpeedLevelBought <= 4)
{
TopSpeedText.text = "Buy Top Speed: " + TopSpeedLevelToBuy;
SteeringText.text = "Buy Steering: " + SteeringLevelToBuy;
} else {
TopSpeedText.text = "Max Top Speed Bought!";
}
}
}
public void PurchaseTopSpeedUpgrade()
{
if (TopSpeedLevelBought == 4 && SpendableXP >= 10)
{
GameObject.Find ("_GameManager").GetComponent<XPandLevels> ().CurrentXpToSpend -= 10;
TopSpeedLevelBought = 5;
TopSpeedText.text = "Max Top Speed Bought!";
Debug.Log("Top Speed Level 5 Bought!");
}
if (TopSpeedLevelBought == 3 && SpendableXP >= 10)
{
GameObject.Find ("_GameManager").GetComponent<XPandLevels> ().CurrentXpToSpend -= 10;
TopSpeedLevelBought = 4;
Debug.Log("Top Speed Level 4 Bought!");
}
if (TopSpeedLevelBought == 2 && SpendableXP >= 10)
{
GameObject.Find ("_GameManager").GetComponent<XPandLevels> ().CurrentXpToSpend -= 10;
TopSpeedLevelBought = 3;
Debug.Log("Top Speed Level 3 Bought!");
}
if (TopSpeedLevelBought == 1 && SpendableXP >= 10)
{
GameObject.Find ("_GameManager").GetComponent<XPandLevels> ().CurrentXpToSpend -= 10;
TopSpeedLevelBought = 2;
Debug.Log("Top Speed Level 2 Bought!");
}
if (TopSpeedLevelBought == 0 && SpendableXP >= 10)
{
GameObject.Find ("_GameManager").GetComponent<XPandLevels> ().CurrentXpToSpend -= 10;
TopSpeedLevelBought += 1;
Debug.Log("Top Speed Level 1 Bought!");
}
}
}
My XPAndLevels script.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class XPandLevels : MonoBehaviour {
//vairables to control the levels for the players
public int StartLevel = 1;
public int MaxLevel = 30;
private UnityEngine.UI.Text CurrentLevelText;
private int CurrentLevel;
private int SavedLevel;
private UnityEngine.UI.Text CurrentXpToNextLevelText;
private int CurrentXpToNextLevel;
private UnityEngine.UI.Text CurrentXPText;
public int CurrentXP;
private UnityEngine.UI.Text CurrentXpToSpendText;
public int CurrentXpToSpend;
//Check if the player has played before.
public int IsNew;
//Here I store the variables for how much XP is needed for each level!
public int XpToLevelTwo = 30;
public int XpToLevelThree = 80;
public int XpToLevelFour = 190;
public int XpToLevelFive = 350;
public int XpToLevelSix = 550;
public int XpToLevelSeven = 780;
private int TopSpeedLevel;
private Text TopSpeedText;
private int SteeringLevel;
private Text SteeringText;
public static XPandLevels Instance;
// Use this for initialization
void Start () {
LoadGame ();
//If the player is new than set their level to the start level (which is 1)
if (IsNew == 0) {
CurrentLevel = StartLevel;
Debug.Log ("Level set to start level!");
IsNew = 1;
} else {
//othweriwse we will load the level that they have saved
CurrentLevel = SavedLevel;
Debug.Log("Level set to saved level!");
}
StartCoroutine (AutoTick ());
}
// Update is called once per frame
void Update () {
if (Application.loadedLevel == 1)
{
if (CurrentXP >= XpToLevelTwo)
{
CurrentLevel = 2;
} if (CurrentXP >= XpToLevelThree)
{
CurrentLevel = 3;
} if (CurrentXP >= XpToLevelFour)
{
CurrentLevel = 4;
} if (CurrentXP >= XpToLevelFive)
{
CurrentLevel = 5;
} if (CurrentXP >= XpToLevelSix)
{
CurrentLevel = 6;
} if (CurrentXP >= XpToLevelSeven)
{
CurrentLevel = 7;
}
if (CurrentLevel == 1)
{
CurrentXpToNextLevel = XpToLevelTwo - CurrentXP;
} if (CurrentLevel == 2)
{
CurrentXpToNextLevel = XpToLevelThree - CurrentXP;
} if (CurrentLevel == 3)
{
CurrentXpToNextLevel = XpToLevelFour - CurrentXP;
} if (CurrentLevel == 4)
{
CurrentXpToNextLevel = XpToLevelFive - CurrentXP;
} if (CurrentLevel == 5)
{
CurrentXpToNextLevel = XpToLevelSix - CurrentXP;
} if (CurrentLevel == 6)
{
CurrentXpToNextLevel = XpToLevelSeven - CurrentXP;
}
//if the loaded level is the main menu than find the text conponents so
//I can display the player level, spendable xp and xp needed to level up.
CurrentXpToSpendText = GameObject.Find("CurrentXP").GetComponent<Text>();
CurrentXpToNextLevelText = GameObject.Find("CurrentXpToNextLevel").GetComponent<Text>();
CurrentLevelText = GameObject.Find("Currentlevel").GetComponent<Text>();
TopSpeedText = GameObject.Find ("TopSpeedUpgrade").GetComponent<Text>();
SteeringText = GameObject.Find ("SteeringUpgrade").GetComponent<Text>();
//write to the text components saying things such as how much XP a player can spend,
//how close they are to the next level and what level they are currently.
CurrentXpToSpendText.text = "Current Spendable XP: " + CurrentXpToSpend;
CurrentXpToNextLevelText.text = "XP Until Next Level: " + CurrentXpToNextLevel;
CurrentLevelText.text = "Current Level: " + CurrentLevel;
TopSpeedText.text = "Top Speed Level: " + TopSpeedLevel;
SteeringText.text = "Steering Level: " + SteeringLevel;
}
TopSpeedLevel = GameObject.Find("_GameManager").GetComponent<Upgrades>().TopSpeedLevelBought;
SteeringLevel = GameObject.Find("_GameManager").GetComponent<Upgrades>().SteeringLevelBought;
}
public void DeleteSave()
{
PlayerPrefs.DeleteKey ("CurrentLevel");
PlayerPrefs.DeleteKey ("CurrentXpToNextLevel");
PlayerPrefs.DeleteKey ("CurrentXp");
PlayerPrefs.DeleteKey ("CurrentXpToSpend");
PlayerPrefs.DeleteKey ("IsNew");
PlayerPrefs.DeleteKey ("TopSpeedLevel");
PlayerPrefs.DeleteKey ("SteeringLevel");
CurrentLevel = 1;
CurrentXP = 0;
CurrentXpToSpend = 0;
IsNew = 0;
GameObject.Find("_GameManager").GetComponent<Upgrades>().TopSpeedLevelBought = 0;
GameObject.Find("_GameManager").GetComponent<Upgrades>().SteeringLevelBought = 0;
Debug.Log ("Save deleted!");
}
public void LoadGame()
{
SavedLevel = PlayerPrefs.GetInt ("CurrentLevel");
CurrentXpToNextLevel = PlayerPrefs.GetInt ("CurrentXpToNextLevel");
CurrentXP = PlayerPrefs.GetInt ("CurrentXp");
CurrentXpToSpend = PlayerPrefs.GetInt ("CurrentXpToSpend");
IsNew = PlayerPrefs.GetInt ("IsNew");
GameObject.Find("_GameManager").GetComponent<Upgrades>().TopSpeedLevelBought = PlayerPrefs.GetInt ("TopSpeedLevel");
GameObject.Find("_GameManager").GetComponent<Upgrades>().SteeringLevelBought = PlayerPrefs.GetInt ("SteeringLevel");
Debug.Log ("Save loaded!");
}
public void AutoSave()
{
PlayerPrefs.SetInt ("CurrentLevel", CurrentLevel);
PlayerPrefs.SetInt ("CurrentXpToNextLevel", CurrentXpToNextLevel);
PlayerPrefs.SetInt ("CurrentXp", CurrentXP);
PlayerPrefs.SetInt ("CurrentXpToSpend", CurrentXpToSpend);
PlayerPrefs.SetInt ("IsNew", IsNew);
PlayerPrefs.SetInt ("TopSpeedLevel", TopSpeedLevel);
PlayerPrefs.SetInt ("SteeringLevel", SteeringLevel);
Debug.Log("Game Autosaved!"); //Prints to console to say it had autosaved
}
IEnumerator AutoTick()
{
while (true)
{
//"5" is the interval of seconds at which it will autosave, just change to a higher/lower
//number for slower/quicker autosaving.
//Lower if the game starts to lag! (Lol)
yield return new WaitForSeconds (1);
AutoSave();
}
}
}
This is a peculiar thing that I have never seen before and any help would be greatly appreciated.
Thanks in advance.
EDIT:
I have a Game Manager game object with these scripts on.
I create it in the splash screen and use DontDestroyOnLoad.
