• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How can I Instantiate in a grid a list of prefabs?

Resolved: How can I Instantiate in a grid a list of prefabs?

0
By Isaac Tonny on 03/04/2023 Issue
Share
Facebook Twitter LinkedIn

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.
How can I spawn 12 prefabs * 2 in a random order in a grid?

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).
If you need to reset the game multiple times I would recommend making a copy of the list, to preserve the cards for next time.
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.
It’s not really important, but you could improve the for loop and make it a bit easier to read by looping thru the x and y of the grid instead of going with a single i and calculating the coordinates every time.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

c# instantiation unity3d
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to stop a thread that has a blocking function from easygui in python

05/04/2023

Resolved: Removing null keys from a json array of objects

05/04/2023

Resolved: How can I generate at compile-time a separate OpenAPI Swagger.json file for each Controller in my ASP.NET project?

05/04/2023

Comments are closed.

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.