Create an obstacles inside the map

Hai, i already create a map like this:

Now i want to draw a obstacles within those grids, how do i do that?

Right now i only be able to draw a obstacles during mouse down, but i am not able to generate a map that have a obstacles once i run the game.

Please help me.

Here is my code for making a grid in unity3d (c#):

public GameObject TilePrefab;
public List<List<Tile>> map = new List<List<Tile>>();
public int mapSize = 20;

void GenerateMap() 
    {
        map = new List<List<Tile>>();

        for (int i = 0; i < mapSize; i++) 
        {
			List <Tile> row = new List<Tile>();

            for (int j = 0; j < mapSize; j++)
            {
                Tile tile = ((GameObject)Instantiate(TilePrefab, new Vector3(i - Mathf.Floor(mapSize / 2), 0, -j + Mathf.Floor(mapSize / 2)), Quaternion.Euler(new Vector3()))).GetComponent<Tile>();
				tile.gridPosition = new Vector2(i, j);
				row.Add(tile);
			}

            map.Add(row);
		}
	}

And here is my code for draw a obstacles once i hit the mouse button (OnMouseDown):

public bool obstacles = false;
void OnMouseDown()
    {
        obstacles = obstacles ? false : true;

        if (obstacles)
        {
            transform.renderer.material.color = new Color(.5f, .5f, 0.0f);
        }

        else
        {
            transform.renderer.material.color = Color.white;
        }
    }

Thank you very much! I appreciate your answer

This may not be the best way to do it, but it should work. (add this to your script)

//the number of obstacles
public int obstacleNumber = 10;

void Start()
{
    GenerateObstacles();
}

void GenerateObstacles()
{
    int currentObstaclesNumber = 0;  //the current number of obstacles created
    int obsX, obsY; //used in the while loop, used to randomly pick a tile
    while(currentObstaclesNumber < obstacleNumber ) //if there aren't enough obstacles in the scene, keep going!
    {
       obsX = Random.Range(1, mapSize) -1;// randomly chose a tile, on the X axis
       obsY = Random.Range(1, mapSize) -1;// and on the Y axis
       if(map[obsX][obsY].renderer.material.color != Color(.5f, .5f, 0.0f)) // check if the tile is already an obstacle (you may want to do this using an array to keep track of the obstacles, or using a script attached to the tile)
       {
            map[obsX][obsY].GetComponent(ScriptAttachedToTile).obstacle = true;
            currentObstaclesNumber+=1;  //increments the number of obstacles
       }
    }
}

After writing this, I realised I have no idea how to work with lists in Unity3D.
I’m using [] to get a tile from the map list.
But as long as you understand how this works, I assume you can change the code so that it works for you!

EDIT: I changed the code a little. In the while loop.
Instead of changing the color, set the variable obstacle to true. I assume you have a script (like the second one in your question) attached to every single tile. Also, you may want to change “OnMouseDown” to “Update”.

Hope it helps!