What I want to do is place one of my already made sprites at the position of the 1’s in the game so that with enough 1’s I could make a good map. (The sprite to go at the 1’s can just be a plain square).
If i want to place a sprite at the values of 1s i would do the following:
// defaine a multi-dimentional array an intialize some indices with 1
int[,] MapArray = new int[5, 5];
MapArray[0, 3] = 1;
MapArray[1, 4] = 1;
MapArray[2, 3] = 1;
MapArray[4, 1] = 1;
// for each row find the value 1 in each column
for (int r = 0; r < MapArray.GetLength(0); r++)
for (int c = 0; c < MapArray.GetLength(1); c++)
{
if (MapArray[r, c] == 1) // if the value of this index is 1
{
// .. Instantiate your sprites here
}
}
//2D Sprite object to spawn in at coordinates
public GameObject blockObj;
//Map height and mapWidth
const int mapWidth = 4;
const int mapHeight = 4;
//Creating the array with width and height of a predefined value
void Start()
{
//Create the map, and update the map
int[,] MapArray = CreateMap();
UpdateMap(MapArray);
}
//Define values where the 1's go here
int[,] CreateMap()
{
int[,] mapArray = new int[mapWidth, mapHeight];
//These values must be within [0,0] - [4,4] for a 5x5 grid
mapArray[0, 1] = 1;
mapArray[1, 1] = 1;
mapArray[2, 1] = 1;
mapArray[3, 1] = 1;
return mapArray;
}
void UpdateMap(int[,] MapArray)
{
//Iterating through the list with 'x' and 'y' representing the x and y cartesian coordinates
for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth; x++)
{
//If the current index has a value of 1
if (MapArray[x, y] == 1)
{
//Create a block at this point
Instantiate(blockObj, new Vector3(x, y, 0), Quaternion.identity);
}
}
}
}