Ok so the question is related to the following problem in C#. It’s not a ‘my code doesn’t work’ question, rather a ‘which way am I supposed to do this’ question.
I have a GameObject which contains data for four players:
ScriptA
int player01Value;
int player02Value;
int player03Value;
int player04Value;
What I need to do is fetch one of these for use in another script, but we don’t know which one until later.
ScriptB
void someMethod()
{
private string fullString;
private int playerNumber = //a number I get from somewhere.
// An int between 01 and 04. In this case 01
fullString = "player" +playerNumber +"value";
// fullString is now: "player01value"
GetTheInfo(fullString);
}
void GetTheInfo(string whichPlayerValue)
{
int aPlayersIntValue = ScriptA.(whichPlayerValue);
//
as you can see, the identifier ‘whichPlayerValue’ needs to be defined during the game, since there are four players and we don’t know which player’s value we need until we are given the integer ‘playerNumber’. The period and bracket identifier is obviously not working so I was wondering if somebody could suggest an alternative way to use fullString as the identifier. It would save me copying huge blocks of code just to change one number in each copy. We can’t just Get the int from ScriptB because this is a much-simplified version of a much larger framework. If I get an answer how to do what I’m asking it would be very much appreciated.