0

enter image description hereI 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

Sarita
  • 51
  • 3
  • 8
  • Please be more specific in order to get a more specific answer. Add screenshots of what you've done and explain what you're trying to do. – Fredrik Schön Jan 03 '19 at 12:28

2 Answers2

0

In order to use InputField you're going to need the UI namespace: using UnityEngine.UI;:

// Find GameObject with InputField component attached
GameObject obj = GameObject.Find("MyObjectWithInputField");

// Get the InputField component from the object
InputField inputField = obj.GetComponent<InputField>();

// Read the input value of the InputField
string text = inputField.text;

I'm not sure what kind of comparison you're looking to do but you simply loop through your InputFields and read the values like shown above. To get all InputFields in the scene you can use

InputField[] allInputFields = FindObjectsOfType<InputField>();

EDIT:

New code edited in to OP, here's a quick annotation and minor edits to explain some errors in the code. Check the TODO:s I've added.

public class YouTryTables : MonoBehaviour 
{
    // TODO: This variable isn't used
    int n = 1;
    public Text x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
    // TODO: These variables aren't used
    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 GenerateTable(int n)
    {
        // TODO: Doesn't need .ToString();, they're already strings
        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++)
        {
            // You loop though all 10 arrays but you assign the same component every time; the one on the GameObject you call MyObjectWithInputField
            // You need to get the InputField from the correct GameObjects. Maybe you want to do this in a `public GameObject inputGameObjects` instead?
            // TODO: Get InputField from correct GameObject
            GameObject obj = GameObject.Find("MyObjectWithInputField");
            allInputFields[i] = obj.GetComponent<InputField>();
        }

        for (int j = 0; j < allAnswers.Length; j++)
        {
            // TODO: ans1 isn't set yet, but you don't need to save it to a variable first, you can
            // simply do allAnswers[j] = n * (j + 1);
            allAnswers[j] = ans1; 
        }

        // TODO: You can remove this if you did allAnswers[j] = n * (j + 1); above
        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);
    }

    // TODO: Typo Comapr -> Compare
    // I changed this method to return a bool if all answers were correct
    public bool ComaprAnswers()
    {
        bool allAnswersCorrect = true;

        // TODO: You want to loop through all questions here
        // for (int i = 0; i < allInputFields.Length; i++)
        // TODO: You're comparing string to int, allAnswers should be set to string[] and its setters be made .ToString()
        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;
            allAnswersCorrect = false;
        }

        return allAnswersCorrect;
    }
}

EDIT2

After seeing the screenshot, this is how you get the answer values:

var answerParent = GameObject.Find("AnswerPanel");
var answerObject = answerParent.trasnform.GetChild(n); // Where n is 0 - 9
var answerValue = answerObject.GetComponent<InputField>().text;
Fredrik Schön
  • 4,888
  • 1
  • 21
  • 32
  • additionally since the OP asks for `compare with another int array` you also need to parse to int e.g. using `Int32.TryParse(inputField.text, out x);` ([more info](https://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int)) – derHugo Jan 03 '19 at 12:51
  • @Maakep sorry for not explaining my problem in detail.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. – Sarita Jan 04 '19 at 08:13
  • I am updating my code which has so many errors at the moment as just trying to show what i am trying to achieve. hope you can guide me on this.thank you very much – Sarita Jan 04 '19 at 08:17
  • Adding the code is great, but could you also include a screenshot explaining how you set up the GameObjects. Like, in the hiererchy; how are the GameObjects placed and what components are on what GameObjects. I'm afraid I will need it in order to help you. If not, I can point out all the errors in your code and what you need to do to fix them. – Fredrik Schön Jan 04 '19 at 11:20
  • I edited my post to include a review of your code, @Sarita – Fredrik Schön Jan 04 '19 at 12:33
  • @Maakep I am going through corrections and suggestions that u have given. also uploading unity screenshot. x1 - x10 are text components to display questions. in Answer panel i have 10 input fields from the array for user to enter answers. check btn will perform comparison with correct answers and user's input .i hope it explains what i m trying to achieve. i will try above code and let u know if it works. thanks a ton – Sarita Jan 05 '19 at 02:59
  • You didn't exand the AnswerPanel's InputField so I'll assume there is nothing of interest under them. I did an EDIT2 on how to get the answer values. – Fredrik Schön Jan 07 '19 at 10:21
  • yes i have placeholder and text as child under each input field. default input field – Sarita Jan 07 '19 at 13:30
0

var ret = FirstArray.Any(x=> SecondArray.Any(y=>x==y))

Idan Marko
  • 94
  • 6