Referencing private string for array name

I have a number of arrays which are randomly selected from an array of array names. I have a function that writes the randomly selected array name to a string called newEq. The next function then references this string as the name of an array. But I keep getting the error “Cannot implicitly convert type char' to string’”.

void pickEquation () {
	newEq = Eqs[Random.Range(0, 50)];
	newEquation();
}

void newEquation () {
	LeftText.GetComponent<TextVariable>().Ringvariable = newEq[0];
	TopText.GetComponent<TextVariable>().Ringvariable = newEq[1];
	RightText.GetComponent<TextVariable>().Ringvariable = newEq[2];
	BottomText.GetComponent<TextVariable>().Ringvariable = newEq[3];
	Equation.GetComponent<TextVariable>().Ringvariable = newEq[17];
}

newEq is string and so newEq is a char . you should convert char to string or call ToString() function of newEq .

Do not use casts here! newEq type is 'string' already.

LeftText.GetComponent().Ringvariable = Eqs[Random.Range(0, 50)];

with or without the "newEq = Eqs[Random.Range(0, 50)];" i have in another void?

newEq = Eqs[Random.Range(0, 50)]; becomes useless if you write like i said.

2 Answers

2

If newEq is of type ‘string’ then

newEq[0]

is grabbing the first char of that string. You can’t assign a char to Ringvariable if Ringvariable is a string as well.

THe char isn't what I want. I want newEq to be a referenceable variable, by which I can select the name of the array I want to use.

Even if your array has ‘string’ inside, you should use cast.

  ... = (string)newEq[0];

But your ‘newEq’ is not array, it’s ‘string’.
If you need random names your code will work that way:

LeftText.GetComponent<TextVariable>().Ringvariable = Eqs[Random.Range(0, 50)];
TopText.GetComponent<TextVariable>().Ringvariable = Eqs[Random.Range(0, 50)];
RightText.GetComponent<TextVariable>().Ringvariable = Eqs[Random.Range(0, 50)];
BottomText.GetComponent<TextVariable>().Ringvariable = Eqs[Random.Range(0, 50)];
Equation.GetComponent<TextVariable>().Ringvariable = Eqs[Random.Range(0, 50)];

void newEquation () { LeftText.GetComponent<TextVariable>().Ringvariable = (string)newEq[0]; TopText.GetComponent<TextVariable>().Ringvariable = (string)newEq[1]; RightText.GetComponent<TextVariable>().Ringvariable = (string)newEq[2]; BottomText.GetComponent<TextVariable>().Ringvariable = (string)newEq[3]; Equation.GetComponent<TextVariable>().Ringvariable = (string)newEq[17]; changed the error message to Cannot convert type char' to string'

Your 'newEq' is string already, so newEq[0] will retunr 'n' newEq[1] will return 'e' newEq[2] will return 'w' etc... I edited my answer, check it out

I need each line of newEquation to reference the same array. this looks like each line chooses a new array.