0
\$\begingroup\$

So I have a tilemap that already has tiles on it. What I want is to clear it all out and create a solid rectangle of one tile, with which the rest of the monobehaviour will 'carve out' to create a cave complex.

So in my Start() I begin with:

tilemap.ClearAllTiles();

tilemap.BoxFill(Vector3Int.zero, wallTile, -mapSize, -mapSize, mapSize, mapSize);

Let's say mapSize is 8, and "tilemap" is a reference to a Tilemap that previously had some tiles on it. I expect to see all the prior tiles removed, and a new filled area of "wallTile" that goes from -8,-8 to 8,8

Instead I get a single tile at 0,0,0 and that's it.

If I leave out the ClearAllTiles(), it does nothing; the old tiles remain, and no new tiles are placed at all. The only way it seems to do anything is if the tilemap was empty before in the scene, but I need to work with a tilemap that was previously in use.

Any suggestions on what I'm doing wrong?

\$\endgroup\$
5
  • \$\begingroup\$ What does your tilemap.cellBounds show? \$\endgroup\$
    – DMGregory
    Aug 19, 2020 at 14:33
  • \$\begingroup\$ Debug.Log(tilemap.CellBounds()) gives Position: (0, 0, 0), Size: (1, 1, 1) So basically the ClearAllTiles is resetting the bounds back to nothing? I tried the two bounds properties on tilemap, but they're read only \$\endgroup\$ Aug 19, 2020 at 14:57
  • \$\begingroup\$ Try editing it into your question so you can add linebreaks without accidentally sending the comment half-finished. ;) \$\endgroup\$
    – DMGregory
    Aug 19, 2020 at 14:58
  • \$\begingroup\$ Looks like I've found a possible work-around, though I don't think it's a good answer. After ClearAllTiles, I did four separate SetTile to put wallTile at the four corners of the box, which resized the bounds manually. Then I did the BoxFill. Though this feels clunky, like there should be a better way to resize the tilemap bounds without needing to place tiles. \$\endgroup\$ Aug 19, 2020 at 15:08
  • 1
    \$\begingroup\$ If you've solved your problem, please post your solution as an answer. Note that you only need to place two diagonally-opposite corners to set your bounds, not all four. \$\endgroup\$
    – DMGregory
    Aug 19, 2020 at 15:16

2 Answers 2

2
\$\begingroup\$

So turns out the tilemap's bounds were being reset to nothing. Any attempt to fill outside of the bounds will fail, and trying to fill on top of a cell that's got a tile will also fail.

The work-around was to clear the tiles, place a tile at the upper left and bottom right of the area I was going to fill ( which would resize the bounds). Then I could box fill the tilemap. There doesn't seem to be a way to enlarge the bounds of the tilemap any other way.

\$\endgroup\$
0
\$\begingroup\$
_tilemapFlour.ClearAllTiles();
_tilemapFlour.origin = Vector3Int.zero;
_tilemapFlour.size = new Vector3Int(_size, _size);
var endPosition = _tilemapFlour.origin + _tilemapFlour.size;
_tilemapFlour.BoxFill(endPosition, _tileBase, _tilemapFlour.origin.x, _tilemapFlour.origin.y, endPosition.x, endPosition.y);
_tilemapFlour.ResizeBounds();
\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can improve your answer by describing what your code does in particular to address OP's issue. \$\endgroup\$
    – liggiorgio
    Nov 1, 2023 at 15:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .