I want to make an array of 10 input fields to get user inputs and then compare with another int array elements.
I am not able to understand that how to call input field child text elements.
Can any one please guide me best possible method to achieve this in unity using c#? Thanks
I am trying to display 10 questions and user has to answer those by entering text in inputfields. I want to make input field array to store users's answers and another array to store correct answers. then i want to compare both when user clicks on check button. if answer is correct i will highlight it with green or else red.
public class YouTryTables : MonoBehaviour{
int n = 1;
public Text x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
public int ans1, ans2, ans3, ans4, ans5, ans6, ans7, ans8, ans9, ans10;
public InputField[] allInputFields = new InputField[10]; //array of user Answers entered in input fields
public int[] allAnswers = new int[10];//array of correct answers
public void Start()
{
}
public void GetInput1(string i)
{
}
public void GenerateTable(int n)
{
x1.text = (n + " X " + 1 + " = ").ToString();
x2.text = (n + " X " + 2 + " = ").ToString();
x3.text = (n + " X " + 3 + " = ").ToString();
x4.text = (n + " X " + 4 + " = ").ToString();
x5.text = (n + " X " + 5 + " = ").ToString();
x6.text = (n + " X " + 6 + " = ").ToString();
x7.text = (n + " X " + 7 + " = ").ToString();
x8.text = (n + " X " + 8 + " = ").ToString();
x9.text = (n + " X " + 9 + " = ").ToString();
x10.text = (n + " X " + 10 + " = ").ToString();
for (int i = 0; i < allInputFields.Length; i++)
{
GameObject obj = GameObject.Find("MyObjectWithInputField");
allInputFields[i] = obj.GetComponent<InputField>();
}
for (int j = 0; j< allAnswers.Length; j++)
{
allAnswers[j] = ans1;
}
ans1 = (n * 1);
ans2 = (n * 2);
ans3 = (n * 3);
ans4 = (n * 4);
ans5 = (n * 5);
ans6 = (n * 6);
ans7 = (n * 7);
ans8 = (n * 8);
ans9 = (n * 9);
ans10 = (n * 10);
}
public void ComaprAnswers()
{
if (allInputFields[i] == allAnswers[j])
{
Text text = allInputFields.transform.Find("Text").GetComponent<Text>();
text.color = Color.green;
}
else
{
Text text = allInputFields.transform.Find("Text").GetComponent<Text>();
text.color = Color.red;
}
}
}
Thanks