I am making a game with upgrades, I have class call “GlobalStats” which contains the static variables I need to reference.
I have made another class “MenuUpgrade” that needs to reference these variables, but I want to define which variables are referenced in the editor and not in code, this is so I don’t have to duplicate everything 50+ times, and hence the class has to be use generic variables without references to particular static variables.
How can I cast a generic variable in my non-static class as a static variable?
public class GlobalStats : MonoBehaviour
{
// Static Variable
public static int maxSpeedLevel = 0;
public static float playerMaxSpeed = 12.5f;
}
public class MenuUpgrade : MonoBehaviour
{
// Generic Variables
public string upgradeLevel;
public string upgradeStat;
}
To get around this problem I created new variables in which to store the value of the static ones.
int iUpgradeLevel = 0;
float iUpgradeStat = 0;
int iUpgradePrice = 0;
Rather than try and reference the “GlobalStats” static variables as strings in my code, I entered a boolean check so that I could toggle in the inspector which set of statics to reference.
public bool bMaxSpeed;
public bool bAcceleration;
public bool bBoostSpeed;
Depending on which bool is checked determines when values are passed into my upgrade menu slot, and which values will be altered by the function.
if(bMaxSpeed == true)
{
iUpgradeLevel = GlobalStats.maxSpeedLevel;
iUpgradeStat = GlobalStats.maxSpeed;
iUpgradePrice = GlobalStats.maxSpeedPrice;
}
if(bAcceleration == true)
{
iUpgradeLevel = GlobalStats.accelerationLevel;
iUpgradeStat = GlobalStats.acceleration;
iUpgradePrice = GlobalStats.accelerationPrice;
}
if(bBoostSpeed == true)
{
iUpgradeLevel = GlobalStats.boostSpeedLevel;
iUpgradeStat = GlobalStats.boostSpeed;
iUpgradePrice = GlobalStats.boostSpeedPrice;
}
These values can then be used as part of a generic upgrade system.
// Deny Upgrade!
if(GlobalStats.cash < iUpgradePrice || iUpgradeLevel == 5)
{
audio.PlayOneShot(deny, 1f);
}
// Buy Upgrade!
if(GlobalStats.cash >= iUpgradePrice && iUpgradeLevel < 5)
{
iUpgradeStat += statAmount;
GlobalStats.cash -= iUpgradePrice;
iUpgradePrice *= 2;
iUpgradeLevel += 1;
bSwitchSprite = true;
audio.PlayOneShot(click, 1f);
audio.PlayOneShot(drill, 1f);
}
if(iUpgradeLevel < 5)
{
currentText.text = currentString + ": " + iUpgradeStat.ToString();
costText.text = iUpgradePrice.ToString();
}
if(iUpgradeLevel >= 5)
{
costText.text = "MAX";
currentText.text = currentString + ": " + iUpgradeStat.ToString();
descriptionText.text = "Fully Upgraded";
}
Once an upgrade has been purchased I then make sure to change the value of the original static to represent there new values.
if(bMaxSpeed == true)
{
GlobalStats.maxSpeedLevel = iUpgradeLevel;
GlobalStats.maxSpeed = iUpgradeStat;
GlobalStats.maxSpeedPrice = iUpgradePrice;
statAmount = 1.25f;
}
if(bAcceleration == true)
{
GlobalStats.accelerationLevel = iUpgradeLevel;
GlobalStats.acceleration = iUpgradeStat;
GlobalStats.accelerationPrice = iUpgradePrice;
statAmount = 0.005f;
}
if(bBoostSpeed == true)
{
GlobalStats.boostSpeedLevel = iUpgradeLevel;
GlobalStats.boostSpeed = iUpgradeStat;
GlobalStats.boostSpeedPrice = iUpgradePrice;
statAmount = 5;
}
This provided me with the solution I was looking for, but require me to use these boolean checks rather than pass in the name of the static variable to use. I’m sorry for the poor phrased question but I hope this helps make sense of what I was asking.