0
\$\begingroup\$

So question is: Why is the unity editor behaving this way (not updating the tilemaps until I go though the scene and click on them) and why is A* not generating the proper grid when Scan() is called from code and only when I use the button in the editor?

So I'm generating a tile map with code and all the generation works... Except when I open up the scene in the editor it looks all messed up, as seen in the First image. The walls are generated but not showing up properly and the A* grid shows everything as walkable. My Scene View having clicked nothing but the scene tab at the top of the editor If I go through and click each tilemap node, as can be seen in the next couple shots the tilemaps show up correctly and visually it looks like it should.

I am calling .RefreshAllTiles(); inside my code on each tilemap when I finish their generation. In fact here is the whole function for the TileWalls tilemap node:

private void FloodWallTiles()
{
    _wallTileMap.size = new Vector3Int(_floorTileMap.size.x + 4, _floorTileMap.size.y + 4, 0);
    _wallTileMap.origin = new Vector3Int(_floorTileMap.origin.x - 2, _floorTileMap.origin.y - 2, _floorTileMap.origin.z);
    _wallTileMap.ResizeBounds();

    int xRightBounds = _wallTileMap.origin.x + _wallTileMap.size.x;
    int yTopBounds = _wallTileMap.origin.y + _wallTileMap.size.y;

    for (int i = _wallTileMap.origin.x; i < xRightBounds; i++)
    {
        for (int j = _wallTileMap.origin.y; j < yTopBounds; j++)
        {
            Vector3Int currentCell = new Vector3Int(i, j, _floorTileMap.origin.z);
            if (_floorTileMap.HasTile(currentCell) || _subWallTileMap.HasTile(currentCell))
            {
                continue;
            }
            else
            {
                _wallTileMap.SetTile(currentCell, _wallTiles);
            }
        }
    }
            _wallTileMap.RefreshAllTiles();
}

My Scene View after clicking on the TileWalls Tilemap node My Scene View after clicking on the SubWall Tilemap node My Scene View after clicking on the TileFloor Tilemap node After doing that, I have to hit scan on my A* node even though I am calling Scan() on my gridGraph in my code, and only doing that AFTER I have generated the tilemaps and moved it into position. See code for that below the final image. My Scene View after clicking on the A* node and also clicking Scan

private void SetAStarPathFinderAttributes()
{
    int width = _wallTileMap.size.x;
    int height = _wallTileMap.size.y;
    float nodeSize = 1;

    Vector3 center = GetTileMapCenter();

    AstarPath sceneAstarComponent = _aStarPathfinderPrefab.GetComponent<AstarPath>();
    GridGraph grid = sceneAstarComponent.data.gridGraph;

    grid.center = center;
    grid.SetDimensions(width, height, nodeSize);
    AstarPath.active.data.gridGraph.Scan();
}

private Vector3 GetTileMapCenter()
{
    int halfWidth = _wallTileMap.size.x / 2;
    int halfHeight = _wallTileMap.size.y / 2;

    Vector3 center = new Vector3(_wallTileMap.origin.x + halfWidth, _wallTileMap.origin.y + halfHeight, _wallTileMap.origin.z);
    return center;
}

My start function where all the methods are being called from:

private void Start()
{
    Directions initialDirection = Directions.UP;
    _fixedRandom = new System.Random(_levelSeed);
    _rooms = new Room[_numberOfRooms];
    for(int i = 0; i < _numberOfRooms; i++)
    {
        int randomHeight = _fixedRandom.Next(7, 21);
        int randomWidth = _fixedRandom.Next(7, 21);
        _rooms[i] = new Room(randomHeight, randomWidth);
    }

    int startX = _fixedRandom.Next(1, _mapSizeSquare);
    int startY = _fixedRandom.Next(1, _mapSizeSquare);

    _startingPoint = new Vector3Int(startX, startY, 0);
    SelectEndingPoint();

    GenerateRooms(0, initialDirection);
    CreateSubWallTiles();
    FloodWallTiles();
    
    GenerateSpawnPrefab();
    GenerateExitPrefab();

    SetAStarPathFinderAttributes();
}

You can see how it behaves in game as I walk around in this video I put on reddit: https://www.reddit.com/r/Unity2D/comments/sezxq6/need_help_troubleshooting_disappearing_tilemap/

\$\endgroup\$
4
  • \$\begingroup\$ Where to you mark your changes to the tilemap to be serialized and saved (using eg. Undo.RecordObject, EditorUtility.SetDirty, etc)? \$\endgroup\$ Commented Jan 26, 2022 at 21:24
  • \$\begingroup\$ @DMGregory, maybe the problem is what your asking, because I'm not sure what you're referring to. Everything is happening at runtime, I'm not saving the tilemaps anywhere or loading them from anywhere currently. \$\endgroup\$ Commented Jan 26, 2022 at 21:27
  • \$\begingroup\$ Where do you call these methods then? Try to give us a Minimal Complete Verifiable Example. \$\endgroup\$ Commented Jan 26, 2022 at 21:28
  • \$\begingroup\$ @DMGregory, Is that what you were looking for? \$\endgroup\$ Commented Jan 26, 2022 at 21:37

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.