In this post, we will see how to resolve How can I Instantiate in a grid a list of prefabs?
Question:
I’m trying to create a memory game with 24 cards where you have find the pairs. So, I have the 12 prefab card objects that are supposed to spawn in a random order into a grid. I’ve searched online and what worked for me was to create the grid using a for loop and Instantiate to spawn the objects in different positions (rows and columns) till I get the 24 objects. The thing is I cannot find a way to spawn the 12 prefabs list and a copy of each.I’ve tried searching for a different method or how to Instantiate a whole list of objects but I’ve only found how to do it creating an index and Random.Range the list, but this is not what I want because I need all the cards and twice each. This is how my code looks right now.
Best Answer:
The code you have written is almost there. After you pick and instantiate the next card you can go into the list and remove the card (at the index or the object itself).Regarding needing each card to be spawned twice, you can just add each card to the list two times. If you want to be lazy like a programmer you can also make the computer do that for you programmatically.
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com