How to compare elements of input field text array with another array of int?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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
c# unity3d
add a comment |
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
c# unity3d
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 S
Jan 3 at 12:28
add a comment |
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
c# unity3d
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
c# unity3d
c# unity3d
edited Jan 5 at 2:59
Sarita
asked Jan 3 at 12:17
Sarita Sarita
215
215
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 S
Jan 3 at 12:28
add a comment |
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 S
Jan 3 at 12:28
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 S
Jan 3 at 12:28
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 S
Jan 3 at 12:28
add a comment |
2 Answers
2
active
oldest
votes
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;
additionally since the OP asks forcompare with another int array
you also need to parse to int e.g. usingInt32.TryParse(inputField.text, out x);
(more info)
– derHugo
Jan 3 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 4 at 8: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 4 at 8: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 S
Jan 4 at 11:20
I edited my post to include a review of your code, @Sarita
– Fredrik S
Jan 4 at 12:33
|
show 3 more comments
var ret = FirstArray.Any(x=> SecondArray.Any(y=>x==y))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022146%2fhow-to-compare-elements-of-input-field-text-array-with-another-array-of-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
additionally since the OP asks forcompare with another int array
you also need to parse to int e.g. usingInt32.TryParse(inputField.text, out x);
(more info)
– derHugo
Jan 3 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 4 at 8: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 4 at 8: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 S
Jan 4 at 11:20
I edited my post to include a review of your code, @Sarita
– Fredrik S
Jan 4 at 12:33
|
show 3 more comments
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;
additionally since the OP asks forcompare with another int array
you also need to parse to int e.g. usingInt32.TryParse(inputField.text, out x);
(more info)
– derHugo
Jan 3 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 4 at 8: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 4 at 8: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 S
Jan 4 at 11:20
I edited my post to include a review of your code, @Sarita
– Fredrik S
Jan 4 at 12:33
|
show 3 more comments
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;
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;
edited Jan 7 at 10:21
answered Jan 3 at 12:28
Fredrik SFredrik S
2,238921
2,238921
additionally since the OP asks forcompare with another int array
you also need to parse to int e.g. usingInt32.TryParse(inputField.text, out x);
(more info)
– derHugo
Jan 3 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 4 at 8: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 4 at 8: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 S
Jan 4 at 11:20
I edited my post to include a review of your code, @Sarita
– Fredrik S
Jan 4 at 12:33
|
show 3 more comments
additionally since the OP asks forcompare with another int array
you also need to parse to int e.g. usingInt32.TryParse(inputField.text, out x);
(more info)
– derHugo
Jan 3 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 4 at 8: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 4 at 8: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 S
Jan 4 at 11:20
I edited my post to include a review of your code, @Sarita
– Fredrik S
Jan 4 at 12:33
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)– derHugo
Jan 3 at 12:51
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)– derHugo
Jan 3 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 4 at 8:13
@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 4 at 8: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 4 at 8:17
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 4 at 8: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 S
Jan 4 at 11:20
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 S
Jan 4 at 11:20
I edited my post to include a review of your code, @Sarita
– Fredrik S
Jan 4 at 12:33
I edited my post to include a review of your code, @Sarita
– Fredrik S
Jan 4 at 12:33
|
show 3 more comments
var ret = FirstArray.Any(x=> SecondArray.Any(y=>x==y))
add a comment |
var ret = FirstArray.Any(x=> SecondArray.Any(y=>x==y))
add a comment |
var ret = FirstArray.Any(x=> SecondArray.Any(y=>x==y))
var ret = FirstArray.Any(x=> SecondArray.Any(y=>x==y))
answered Jan 3 at 12:53
Idan MarkoIdan Marko
92
92
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54022146%2fhow-to-compare-elements-of-input-field-text-array-with-another-array-of-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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 S
Jan 3 at 12:28