Have searched in other questions but haven't found how to do in c#. Tried using the Map function, but it's not availabe in c#.
I want to use a for loop to refer to values in objects in StrainsManager Class:
public StrainsManager Ba2;
public StrainsManager Ba3;
public StrainsManager Ba4;
public StrainsManager Ba5;
public StrainsManager Ba6;
public StrainsManager Ba7;
public StrainsManager Ba8;
void Update()
{
for (int i = 2; i < 9; i++)
{
string strainString = "Ba" + i;
PlayerPrefs.SetFloat(strainString + "Cost", strainString.cost);
PlayerPrefs.SetFloat(strainString + "IPSAdd", strainString.IPSAdd);
PlayerPrefs.SetFloat(strainString + "Time", strainString.time);
PlayerPrefs.SetFloat(strainString + "CostMod", strainString.costMod);
PlayerPrefs.SetFloat(strainString + "InfectMod", strainString.infectMod);
}
}
Without the for loop I would have to type in 5 lines per object:
void Update()
{
PlayerPrefs.SetFloat("Ba2Cost", Ba2.cost);
PlayerPrefs.SetFloat("Ba2IPSAdd", Ba2.IPSAdd);
PlayerPrefs.SetFloat("Ba2Time", Ba2.time);
PlayerPrefs.SetFloat("Ba2CostMod", Ba2.costMod);
PlayerPrefs.SetFloat("Ba2InfectMod", Ba2.infectMod);
}
Obviously the second part in the first code does not work(strainString.cost) because strainString is a string and not the object of class StrainsManager. Is the above for loop possible or do I have to do it manually typing 35 lines for those 7 objects?
List<StrainsManager>orStrainsManager[]instead of declaring them explicitly. Then just loop over the list/array.Ba2,Ba3, etc.) then you are probably doing it wrong. You should probably be using an array, list, or other data structure to hold all of those variables. It is much easier to deal withBa[n]because you can substitute a variable forn.Ba2wouldn't really mean anything to me if I had to jump onto your team and maintain this code. Sure, intellisense helps to an extent, but that's not good enough IMO.