0

I have a script in which I have to instantiate 100 cards on 100 different locations which are generated by the script ie 10 rows and 10 columns. so when I run play it generates 100 different cards on each of the 100 different locations that means it generates 10,000 cards. all I need to do is generate 100 cards on the 100 positions. I am new bie to programming so I am sure its a dumb one for all of you pros but any assistance is good my script is as below:

'''

private void GenerateGrid ()

{
    {

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                float posX = col * tilesize;
                float posY = row * tilesize;
                {
                    float xoffset = -4f;
                    float yoffset = 4f;

                    transform.position = new Vector2 (posX + xoffset, posY + yoffset);


                   foreach (string card in deck) {


                        GameObject newCard = (GameObject)Instantiate (cardPrefab, transform.position, (Quaternion.identity));

                        //GameObject newCard = (GameObject)Instantiate (cardPrefab);


                        newCard.name = card;
                    }


                }

            }

        }
    }

1 Answers1

0

Think about it. You have 10 rows, 10 columns, 100 cardS. You have 3 for loops nested, so you end up calling GameObject newCard = (GameObject)Instantiate (cardPrefab, transform.position, (Quaternion.identity)); 10*10*100 = 10 000 times.

You don't need to loop through cards actually. You need to loop through rows and columns (which you are already doing) but instead of looping through cards you just want to add one card to that place, each time selecting next card. So you need to have counter variable that counts which card to select next.

    int counter = 0;
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            float posX = col * tilesize;
            float posY = row * tilesize;
            {
                float xoffset = -4f;
                float yoffset = 4f;
                transform.position = new Vector2 (posX + xoffset, posY + yoffset);

                GameObject newCard = (GameObject)Instantiate (cardPrefab, transform.position, (Quaternion.identity));
                // Select card from deck
                newCard.name = deck[counter];
                // Increase counter by 1
                counter++;

            }
        }
    }

Now if you want to be cool programmer, you might realise that your first two for loops can act as counter for you so you don't need that variable. Essentially, you have 2D grid (rows x cols) and you need to map it to 1D string array. Here is another post that explains how to work it out

You can now rewrite my code above to this:

    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            float posX = col * tilesize;
            float posY = row * tilesize;
            {
                float xoffset = -4f;
                float yoffset = 4f;
                transform.position = new Vector2 (posX + xoffset, posY + yoffset);

                GameObject newCard = (GameObject)Instantiate (cardPrefab, transform.position, (Quaternion.identity));
                // Select card from deck
                // Multiplied by "10" because that's width of your row
                newCard.name = deck[row * 10 + col];

            }
        }
    }
rCgLT
  • 128
  • 6
  • Thank you very much for the assistance it worked like a charm and lol @ being a cool programmer !!! I tried being one and it worked. – Jawad Liaqat May 11 '20 at 17:07
  • Ok I am kinda stuck on an other thing I would like your assistance on. so we have instantiated cards in a random order but I want to instantiate them in a certain order in the rows and columns mentioned. please download the image from the link below that gives the orders of the cards. There are 8 decks of 12 cards each numbered 1 to 8. the colored box is where the deck starts from https://wetransfer.com/downloads/b480bd5fb2579c2d995b2db6dd39bb8920200516144409/0dda4188f8e1845030aec83121cff4d420200516144440/ec16a8 – Jawad Liaqat May 16 '20 at 14:48
  • Image can't be downloaded. Link is broken. Cards are not in random order, it goes through your deck variable from the beginning in increments of one. Cards are instantiated in same order they are in that cards array. You need to go to place where you create (instantiate) that "cards" variable and make sure it follows order you want. – rCgLT May 17 '20 at 18:05
  • thanks but the lay out aint that easy I just learnt how to share image on stack over flow. Lemme explain there are a total of `100 cards for of them are just corners like the jokers. the remaining 96 are 8 sets of a 12 cards each . in the image you would see where each set starts from so sorta stuck there – Jawad Liaqat May 17 '20 at 20:45
  • thanks @rCgLt I was Able to use 2 d 3 d arrays and resolve the issue to identify exact spots and used your cool programming way to for the placement of cards thanks though – Jawad Liaqat May 18 '20 at 21:25