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!