For loop problem

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. :slight_smile:

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

Okay, so you want index - 15?
Sorry I didn’t understand at first… I had a good idea, but just wasn’t certain from the way you had written. :slight_smile:

You could use any of these:

  1. array[i - 15]
  2. local int as: int n = i - 15; array[n]
  3. outside loop, declare an int: int n = 0; each iteration, ++n;

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).

Man, you guys are good. I don’t understand anything in this thread.

1 Like

can you explain with code? because I really don’t understand. In the last message ‘i’ was 15

But I hope you understand my problem, Right?

Oh boy… look at your own first post, the second loop, it says:

for (int i = 15; i < cardCollection.Count; i++) {

lol, indeed there is some overflow of confusion here.

Here:

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 ();
   }
}

You shouldn’t mix apples and oranges. Split the array into two, fixing the game now will save you a lot of troubles in the future.

Yes. Now it works. Thanks for help

2 arrays might have been easier. Nevertheless, I’m glad that you got it fixed. =)