Hi. I have two “for loop” for one array. the first 15 are picture cards, the last 15 are word cards. so the array size is 30. now, I wrote two for loop to the array but first 15 is works, the last 15 is does not work. because the answer array size is 15, it sees only the first 15 but it does not see the rest
for (int i = 0; i < questionData.answer.Length; i++) {
if (cardCollection [i].name.Contains ("Fish")) {
Debug.Log ("das");
cardCollection [i].GetComponent<UnityEngine.UI.Image> ().sprite = Resources.Load<Sprite> (questionData.answer [i]);
}
}
for (int i = 15; i < cardCollection.Count; i++) {
for (int n = 0; n < questionData.answer.Length; n++) {
if (cardCollection [i].childCount > 1) {
Debug.Log ("asd");
cardCollection [i].GetComponentInChildren<UnityEngine.UI.Text> ().text = questionData.answer [n].ToString ().ToUpper ();
}
}
}
I wrote the code above but what is the last answer, it writes. I mean 15th element is “bear”. it writes “bear” to the last 15’s text component. anyone how can i fix this? I don’t want to make two array, If i do this, the game maybe can problem
I’d like to help you out, but I’m not confident that I understand your question. If there’s anyway that you think you could rephrase the issue, that could be helpful.
I have an array, sized 30. the first 15 is pictures, the last 15 is words. this is a memory match game actually. Also I have answer array, sized 15. now pictures and words loading according to answer array but answer array size 15, so first 15 does work but the last 15 does not work. because for loop does not go up from 15 because of answer array. I wrote this code above but what is the last answer, it writes. I said already this
I did not understand. I did that you gave me code but it does not work. it writes same word to the last 15 or I didn’t fully understand
for (int i = 0; i < cardCollection.Count; i++) {
for (int n = i -15; n < questionData.answer.Length; n++) {
if (cardCollection [i].childCount > 1) {
Debug.Log ("asd");
cardCollection [i].GetComponentInChildren<UnityEngine.UI.Text> ().text = questionData.answer [n].ToString ().ToUpper ();
}
}
}
well, in your code you started i at ‘0’ (zero) instead of 15, as in your previously posted code?
anyways, that is not important. you don’t need the second loop. Just whenever you need ‘n’ as an index, use i - 15 (where ‘i’ begins at 15… and goes 'til 29).
for (int i = 15; i < cardCollection.Count; i++)
{
int n = i - 15;
if (cardCollection [i].childCount > 1)
{
Debug.Log ("asd");
cardCollection [i].GetComponentInChildren<UnityEngine.UI.Text> ().text = questionData.answer [n].ToString ().ToUpper ();
}
}