Mouse Position to 2D array

I’m currently working on something where you are able to place buildings and roads. The level in which you are able to build is made using a 2D array like this:

``````int[,] map = new int[10,10] {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

void BuildMap()
{
for(int x = 0; x < MapWidth; ++x)
{
for(int y = 0; y < MapHeight; ++y)
{
GameObject Tile = (GameObject)Instantiate(Resources.Load("Tiles/Tile" + map[x,y]), transform.position, Quaternion.identity);
Tile.transform.position = new Vector2((x * TileSize - (MapWidth / 2) * TileSize) + TileSize / 2, -(y * TileSize - ((MapHeight / 2) * TileSize) - TileSize / 2));
Tile.transform.parent = transform;
}
}
}
``````

This will generate a level made of tiles where 0 is grass and 1 rock. But I don’t want to give every tile a collider to check if the player is capable of placing a building using an overlappoint.
What I want to do is when you want to place a building it checks the 2D array if you’re allowed to place something and replaces that number in the array with an new number for the building. So my question is: How do I convert the mouse position to the right position in the 2D array, check it and replace it when allowed?

I found the solution to my problem. Nothing visually has been done yet but this is what the code looks like right now:

``````public class Grid : MonoBehaviour
{
public float snapValue = 1;

public int MapWidth;
public int MapHeight;
public int[,] map;

private Vector2 mousePos;

void Start ()
{
map = new int[MapWidth, MapHeight];
}

void Update()
{
if(Input.GetMouseButtonDown(0))
{
GetGridPoint();
}
}

void GetGridPoint ()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

float snapInverse = 1/snapValue;
float x, y;

x = Mathf.Round(mousePos.x * snapInverse) / snapInverse;
y = Mathf.Round(mousePos.y * snapInverse) / snapInverse;

mousePos = new Vector2(x, y);

if(map[(int)mousePos.x, (int)mousePos.y] == 1)
{
print ("Can't place an object here.");
}
else
{
print ("Place Object.");
PlaceObject(1);
}
}

void PlaceObject(int id)
{
map[(int)mousePos.x, (int)mousePos.y] = id;
}
}
``````