For Loop starting with the last iteration

Hello everyone !

I am a bit a beginner in using Unity and C#, watched/followed some vid’s tutorials and decided to make the first step of making my card game.

But I have a probleme when I instantiate my cards in a “for loop”. Here is the code :

public class CreatingCards : MonoBehaviour
{

    public GameObject peopleCard;
    public GameObject samouraiCard;
    public GameObject oldMadManCard;
    public GameObject deck;

    private Text peopleCardText;
    private Text samouraiCardText;
    private Text oldMadManCardText;


    public List<GameObject> cards = new List<GameObject>();
      

    void Start()
    {
        for (int i = 1; i <= 18; i++)
        {

            GameObject peopleCardInstantiated = Instantiate(peopleCard, new Vector3(0, 0, 0), Quaternion.identity);
            peopleCardInstantiated.transform.SetParent(deck.transform, false);

            peopleCardText = peopleCard.GetComponentInChildren<Text>();
            peopleCardText.text = i.ToString();

            peopleCardInstantiated.transform.position = deck.transform.position;


            cards.Add(peopleCardInstantiated);

            Debug.Log(i);
        }
    }
}

The thing is when I add the card in my cards’ list the first one is the 18th then 1, 2, 3, 4, …
And so in my GameObject Deck, the first one is also the 18th,

So I really don’t understand why is it doing like this because inside the Log I can see he finished with the i = 18.

Thanks in advance for responding me !

Have a good what you want haha

Are you talking about which card, or where it is placed?

If it is which card, line 22 is ALWAYS making a peopleCard, so it will always be a people card.

If you are talking about where, line 28 is the only thing I see positioning the card, and it ALWAYS places it in the same spot. Do you mean to position it in different places?

ALSO, generally the correct form for numbering in computers is zero-based, not 1-based. That means 18 cards would be numbered from 0 to 17, NOT as 1 to 18. But for display purposes it is common to start with 1, and I don’t think this is any factor in the code above since you’re not presently using i to index any collections. ALL collections are zero-based.

1 Like

Hello !

Thanks for the answer !

I know about the correct form for the “for loop” that is zero-based but I needed to start at 1 I think.
It’s not about the positioning in the game either

Actually my code is doing exactly what I need.

1* He creates 18 clone of my prefab peopleCard that I already created in my project.
2* I use the i indicator to display the number of the card which is very important in my game (and I thought it was the simplest way to do it).
3* Then he put it in the parent Desk which is a GameObject already setup in my game, so my peopleCards are his child.
4* And finally put in a the cards’ list which I know that index begin at 0.

The struggle here is about the two last operation I think.

In the Hierarchy when I open the Desk parent, I hope to see my card in the right order (which I recognized with the text component I put in it which I modify with the i indicator) :
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18

But instead I got :
18, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 (So the first one is the 18th and last one the 17th)

At this stage I was not worried by that because I will shuffle the Deck (so the 18 cards) to draw it after.
But the problem is that I have exactly the same result in my cards list that I put in public to check if everything is correct.
So at the index 0 it should be the first card, so the card number 1 instead it’s the 18th card at the index 0.
So I have got : 0-18, 1-1, 2-2, 3-3, 4-4, 5-5, etc.

So it’s like the “for loop” start with the last iteration with i = 18 and then i = 1, i = 2, etc.

But like I said when I Debug.Log it, the iteration of i = 18, so the legally last one is actually iterated in last.

(Sorry about my english that’s why I try to explain as clear as possible)

Thanks for the time you are according to me !

Is it intended that line 25 gets the Text component from “peopleCard” instead of “peopleCardInstantiated”?

3 Likes

Thanks a lot Havokki !

I don’t know why I decided to take the text component of the prefab and not the instantianted one. I’m a dumby haha

Now it’s working perfectly !

Again Thanks !