- Home /
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[i] is a char . you should convert char to string or call ToString() function of newEq[i] .
Your idea got rid of the error, mujpir, but the process still does not apply the array strings to Ringvariable.
New Code: void pickEquation () { newEq = Eqs[Random.Range(0, 50)]; newEquation(); }
void newEquation () {
LeftText.GetComponent<TextVariable>().Ringvariable = (string)newEq[0].ToString();
TopText.GetComponent<TextVariable>().Ringvariable = (string)newEq[1].ToString();
RightText.GetComponent<TextVariable>().Ringvariable = (string)newEq[2].ToString();
BottomText.GetComponent<TextVariable>().Ringvariable = (string)newEq[3].ToString();
Equation.GetComponent<TextVariable>().Ringvariable = (string)newEq[17].ToString();
}
Do not use casts here! newEq type is 'string' already.
Removed the casts, and noticed that I didn't have pickEquation initialized. Now it returns 'E' 'Q' '3' in the texts. I imagine its because I converted newEq to a string, but I don't know how to fix it. Code: void pickEquation () { newEq = Eqs[Random.Range(0, 50)]; newEquation(); }
void newEquation () {
LeftText.GetComponent<TextVariable>().Ringvariable = newEq[0].ToString();
TopText.GetComponent<TextVariable>().Ringvariable = newEq[1].ToString();
RightText.GetComponent<TextVariable>().Ringvariable = newEq[2].ToString();
BottomText.GetComponent<TextVariable>().Ringvariable = newEq[3].ToString();
Equation.GetComponent<TextVariable>().Ringvariable = newEq[17].ToString();
}
}
LeftText.GetComponent().Ringvariable = Eqs[Random.Range(0, 50)];
Answer by flaviusxvii · Dec 10, 2013 at 04:57 PM
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.
Answer by StormSabotage · Dec 10, 2013 at 04:58 PM
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.
Your answer
Follow this Question
Related Questions
how to randomly pick a string from an array 3 Answers
changing GUI Button text with a string array 2 Answers
Assigning array element to variable 1 Answer
pick a random int with the value of 1 from an array 2 Answers
How to randomize an array of Strings? 2 Answers