- Home /
Correct use of setTile to add tile to Tilemap
I've been trying to figure out the correct way to have my player object lay tiles as it moves through a tilemap (it's my first go at a Unity project). Here is a script outline that I thought was on the right track:
private Grid grid;
private Tilemap floor;
[SerializeField] private GameObject BombPrefab;
private Tile _bomb;
...
void Start()
{
grid = FindObjectOfType<Grid>();
floor = GameObject.Find("Floor").GetComponent<Tilemap>();
}
...
void update()
{
startCoord = grid.WorldToCell(transform.position);
...
else if (Input.GetKeyDown ("l"))
{
_bomb.gameObject = Instantiate(BombPrefab, transform.position, Quaternion.identity).GetComponent<GameObject>();
_bomb.sprite = _bomb.gameObject.GetComponent<Sprite>();
floor.SetTile(startCoord, _bomb);
}
...
}
This does place a bomb tile below my player, however I have come to realize that this is not a part of the floor
Tilemap. I can comment out both the _bomb.sprite = ...
line and the floor.SetTIle(...
line and it does the same thing. What I would like it to create an actual Tile object with the BombPrefab
sprite attached to it then lay that tile into the actual floor
Tilemap (then presumably it would inherit the Tilemap properties, like sort order, etc.). What am I doing wrong?
Your answer
Follow this Question
Related Questions
How to create tile prefabs? 1 Answer
How can I access to a tile properties in a Tilemap? 0 Answers
Falling Tiles 1 Answer
Can a tile move in real time toward a certain position in the scene? 1 Answer
2D Tiles and sprites 1 Answer