- Home /
Equivelant "set" method for "getInstantiatedObject"?
I'm using a script to generate the map for a game on a Tilemap. I want to randomly populate tiles on the map with game objects, but can't get it working quite right. I have objects being created at random and being placed in seemingly the correct places, but they're not actually associated with the tile that they are on, they just share its coordinates in the world. The most confusing part is that in the official Unity documentation for Tilemap there is a "getInstantiatedObject" method that takes the coordinates of a tile within a tilemap and returns the object instantiated by that tile (Unity Documentation - getInstantiatedObject), but there seems to be no equivalent set method to associate an object with a tile in the first place. So my question is "how do I instantiate an object on a tile so that I can call getInstantiatedObject on that tile to get a reference to the object instantiated by it"?
This is the relevant code in my script:
public int[,] terrainMap;
public Vector3Int tmapSize;
public Tilemap tileMap;
public GameObject obstaclePrefab;
.
.
.
if (terrainMap[x, y] == blocked)
{
GameObject obstacle = Instantiate(obstaclePrefab) as GameObject;
Vector3 tilePos = tileMap.GetCellCenterWorld(new Vector3Int (-x + width / 2, -y + height / 2, 0));
obstacle.transform.position = tilePos;
}
.
.
.
}
I'm still pretty new to unity so apologies for this inevitably being an insultingly simple question, and thanks in advance to anyone who can offer up a potential solution.