- Home /
How do I place sprites with my script and a 2D array?
Lets say I create a 5x5 2D array. With a few randomly chosen elements to equal 1.
int[,] MapArray = new int[4, 4];
MapArray[0, 3] = 1;
MapArray[1, 4] = 1;
MapArray[2, 3] = 1;
MapArray[5, 1] = 1;
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).
Answer by Omar47i · Nov 22, 2016 at 02:12 PM
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
}
}
Yes, this is what I'm looking for but how do I instantiate my sprite (line 14)? Can you explain the code as well please.
I think you should instantiate the sprites as follows:
Instantiate(yourPrefab, new Vector3(r, c, 0), Quaternion.identity);
Can you also explain what that line of code means? aka new Vector represents the position of where to place the sprite, so what does yourPrefab and Quaternion.identity mean?
Answer by AidanHorton · Nov 22, 2016 at 02:25 PM
Edit - Update the code to work properly
//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);
}
}
}
}
I copied and pasted it and there appears to be quite a few errors in it that appeared.
I've imported the script and there were a few issues, but mainly because the code wasn't put into any methods which I assumed you might have done with it :) Anyways, I've got the updated code here. Don't forget to put this code inside your own script in it's own class. Also in the inspector you need to assign the block object you want to spawn in, as the gameobject in the script. I've also update the code in the original post //2D Sprite object to spawn in at coordinates public GameObject blockObj;
//$$anonymous$$ap height and mapWidth
const int mapWidth = 4;
const int mapHeight = 4;
void Start()
{
//Create the map, and update the map
int[,] $$anonymous$$apArray = Create$$anonymous$$ap();
Update$$anonymous$$ap($$anonymous$$apArray);
}
//Define values where the 1's go here
int[,] Create$$anonymous$$ap()
{
int[,] mapArray = new int[mapWidth, mapHeight];
//These values must be within [0,0] - [4,4] for a 5x5 grid
mapArray[0, 3] = 1;
mapArray[1, 4] = 1;
mapArray[2, 3] = 1;
mapArray[4, 1] = 1;
return mapArray;
}
void Update$$anonymous$$ap(int[,] $$anonymous$$apArray)
{
//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 ($$anonymous$$apArray[x, y] == 1)
{
//Create a block at this point
Instantiate(blockObj, new Vector3(x, y, 0), Quaternion.identity);
}
}
}
}
Your answer
Follow this Question
Related Questions
2D sprite wavering 0 Answers
Get "on screen" color of a texture to use in a legend 2 Answers
How to add sign interaction? 1 Answer
How can I change sprites in animation clip? 0 Answers
Getting last sprite from prefab. 1 Answer