duplicate
Same float for the same buttons.
I'm making an idle game, for each upgrade button I am using the same floats, not creating another Script file or I'd have millions of them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UpgradeEpherumDrillers : MonoBehaviour {
public GatherButton g;
public UnityEngine.UI.Text UpgradeNameText;
public UnityEngine.UI.Text UpgradeAmountText;
public UnityEngine.UI.Text UpgradeInfoText;
public UnityEngine.UI.Slider TimerSlider;
public GameObject SliderToDisable;
[Header("Drill Properties")]
public string DrillerName = "";
public float UpgradeCost = 500;
public float UpgradeEpherumPerSecBy = 0;
public float LevelNeededToUnlock = 0;
[HideInInspector]
public float EpherumPerSec = 0;
[HideInInspector]
public float UpgradeAmount = 0;
[Space]
[Header("Timer")]
public float AutoTimerReset = 0;
public float AutoTimer = 0;
void Start(){
UpgradeCost = PlayerPrefs.GetFloat ("UpgradeUpgradeCost", UpgradeCost);
UpgradeEpherumPerSecBy = PlayerPrefs.GetFloat ("UpgradeUpgradeEpherumPerSecBy", UpgradeEpherumPerSecBy);
LevelNeededToUnlock = PlayerPrefs.GetFloat ("UpgradeLevelNeededToUnlock", LevelNeededToUnlock);
EpherumPerSec = PlayerPrefs.GetFloat ("UpgradeEpherumPerSec", EpherumPerSec);
UpgradeAmount = PlayerPrefs.GetFloat ("UpgradeUpgradeAmount", UpgradeAmount);
AutoTimerReset = PlayerPrefs.GetFloat ("UpgradeAutoTimerReset", AutoTimerReset);
AutoTimer = PlayerPrefs.GetFloat ("UpgradeAutoTimer", AutoTimer);
}
void Update () {
PlayerPrefs.SetFloat ("UpgradeUpgradeCost", UpgradeCost);
PlayerPrefs.SetFloat ("UpgradeUpgradeEpherumPerSecBy", UpgradeEpherumPerSecBy);
PlayerPrefs.SetFloat ("UpgradeLevelNeededToUnlock", LevelNeededToUnlock);
PlayerPrefs.SetFloat ("UpgradeEpherumPerSec", EpherumPerSec);
PlayerPrefs.SetFloat ("UpgradeUpgradeAmount", UpgradeAmount);
PlayerPrefs.SetFloat ("UpgradeAutoTimerReset", AutoTimerReset);
PlayerPrefs.SetFloat ("UpgradeAutoTimer", AutoTimer);
if (LevelNeededToUnlock <= g.CurrentLevel) {
UpgradeNameText.text = DrillerName;
UpgradeAmountText.text = "Level " + UpgradeAmount + " " + "Cost " + UpgradeCost;
UpgradeNameText.rectTransform.offsetMin = new Vector2 (UpgradeNameText.rectTransform.offsetMin.x, UpgradeNameText.rectTransform.offsetMin.x + 40);
SliderToDisable.SetActive(true);
UpgradeNameText.color = Color.white;
} else if (LevelNeededToUnlock >= g.CurrentLevel) {
UpgradeNameText.text = "Level Required " + LevelNeededToUnlock;
UpgradeAmountText.text = "";
UpgradeInfoText.text = "";
UpgradeNameText.rectTransform.offsetMin = new Vector2 (UpgradeNameText.rectTransform.offsetMin.x, UpgradeNameText.rectTransform.offsetMin.x);
SliderToDisable.SetActive(false);
}
if (LevelNeededToUnlock <= g.CurrentLevel) {
if (UpgradeAmount == 0) {
UpgradeInfoText.text = "Not Bought";
} else if (UpgradeAmount >= 0) {
UpgradeInfoText.text = "Epherum Per Second + " + UpgradeEpherumPerSecBy;
}
}
if (UpgradeAmount == 0) {
SliderToDisable.SetActive (false);
AutoTimer = AutoTimerReset;
} else if (UpgradeAmount >= 0) {
SliderToDisable.SetActive (true);
}
TimerSlider.minValue = 0;
TimerSlider.maxValue = AutoTimerReset;
TimerSlider.value = AutoTimer;
AutoTimer -= Time.deltaTime;
if (UpgradeAmount >= 0) {
if (AutoTimer <= 0) {
g.EpherumAmount += EpherumPerSec;
AutoTimer += AutoTimerReset;
}
}
}
public void UpgradeBought(){
if (g.CurrentLevel >= LevelNeededToUnlock) {
if (g.EpherumAmount >= UpgradeCost) {
UpgradeAmount += 1;
g.EpherumAmount -= UpgradeCost;
UpgradeCost += Mathf.Lerp (UpgradeCost, UpgradeCost, UpgradeCost);
g.EpherumPerSecondAmount += UpgradeEpherumPerSecBy;
EpherumPerSec += UpgradeEpherumPerSecBy;
}
}
}
}
When I try and save the floats with PlayerPrefs, when I load up the game all the buttons have the same cost, upgrade amount and the same epherumpersec.
How do I make it where they do not save for each button?
Example: I have a IronDrill and a CopperDrill, they both use the same floats and the same everything basically, just different prices and different epherumpersec. but when I save them they all have the same upgrade cost and the same epherumpersec. Thanks for the help :^]
Follow this Question
Related Questions
How to set float, bool from one script to the other? 1 Answer
,Attack and chase range issue 2 Answers
How to script float the object for add to state machine 0 Answers
Camera FX to fade out back to normal 0 Answers
2D drag script Need Help! 1 Answer