How to add a gameobject to a tile at runtime

I have a tilemap and what I am trying to do is instantiate prefabs on the tiles via mouse click. What I then want to do is find a way to prevent the user from instantiating the same prefab at the same location in the tilemap. One way I thought would work was to assign the instantiated prefab to the tile using tile.gameobject, but what happens is all similar tiles get the prefab added to it at runtime. Here is my code:

void OnMouseDown(){
		
		Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		Vector3Int coordinate = currentMap.WorldToCell(mouseWorldPos);    			
			
		Tile t = currentMap.GetTile (coordinate) as Tile;
			
		if (!t.gameObject) {
			
			t.gameObject = Instantiate (infoPanelTemplate) as GameObject; 
			
			GameObject UI = t.gameObject;
			UI.transform.SetParent (currentMap.transform);				
			UI.transform.localPosition = new Vector3 (coordinate.x, coordinate.y, coordinate.z);
			UI.transform.localScale = new Vector3 (0.01f, 0.01f);
			UI.transform.position += Vector3.up * 0.4f;
			UI.transform.position += Vector3.right * 0.01f;
			UI.transform.position += Vector3.back * 0.4f;	
				
		}
	}

Any help is appreciated!

I do have a collider on the tilemap. I’m not sure if having each tile with its’ own collider would affect performance. I decided not to use the array since I didn’t want to have to iterate over the array each time I place a gameobject. As the array grows bigger, the longer it would take to go through the array.

Easy…

come on… you can not think this?

You only need to decide before istantiate if there is any other object there, for example using colliders, or having a VEctor2 array with th grid positions of all objects, and just check if that vector2 position already exists

Bye