- Home /
Trying to Create a grid for a matching game
So I am trying to create a grid for a matching game. I want to take eighteen prefabs and spawn them in a six by six grid so that there are only two of each.
Here is the code that I use to spawn just a regular six by six grid of just plain old 100x100 square sprites:
GameObject prefab;
[HideInInspector]
public GameObject gridHolder;
public int rows = 6;
public int cols = 6;
float spacer = 0.1f;
public static GridManager instance;
void Start()
{
instance = this;
prefab = Resources.Load<GameObject> ("Prefabs/Square");
defaultMat = prefab.GetComponent<SpriteRenderer> ().sharedMaterial;
gridHolder = new GameObject();
gridHolder.name = "Grid_Holder";
BuildGrid ();
}
void BuildGrid()
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
GameObject square = Instantiate (prefab, gridHolder.transform);
Vector2 newPos = new Vector2 (gridHolder.transform.position.x + j + (spacer*j), gridHolder.transform.position.y + i + (spacer*i));
square.transform.position = newPos;
square.name = "Square_" + i + "_" + j;
square.GetComponent<SquareScript> ().row = i;
square.GetComponent<SquareScript> ().col = j;
}
}
gridHolder.transform.position = new Vector2 (-2.75f, -2.75f);
}
My question is, how can I alter this to not only pull from multiple different prefabs but also make sure to use all of them twice, but no more than twice each? I'm looking for the most efficient way to do this.
Your answer
Follow this Question
Related Questions
Grid Based Map Object-Array vs Arrays only 1 Answer
Adding prefabs to a list or an array from a folder and instantiating them. 2 Answers
Put prefabs in a List or arrayList or whatever and call any one of them up 1 Answer
Rotating a tilemap with ruletiles 0 Answers
Problem with arrays and while loops 0 Answers