Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Resolved Null TileBase[] from a non-empty tilemap.

Discussion in 'Scripting' started by umbciok, Apr 1, 2023.

  1. umbciok

    umbciok

    Joined:
    Nov 20, 2021
    Posts:
    4
    I am working on a 2D Isometric game based on TileMaps and I have this bug:


    Code (CSharp):
    1. BoundsInt unlockedArea = temp.unlockedArea;
    2.         BoundsInt buildingArea = temp.area;
    3.  
    4.         TileBase[] checkIfUnlockedArray = availableLandTileMap.GetTilesBlock(buildingArea);
    5.         TileBase[] baseArray = mainTileMap.GetTilesBlock(buildingArea);
    After running this code baseArray[] is filled with correct tiles from mainTileMap.
    And checkIfUnlockedArray[] is filled with nulls - and that's the problem. The buildingArea on this Tilemap consists of tiles that have been put there by the same script earlier on. These tiles are visible in Scene view in Unity.

    However, if I put the same tiles in the same place manually before running the game (using Tile Palette) everything works as expected, the array is filled with the tiles.

    I don't understand what is the problem, since everything on the mainTileMap is detected as it should be (no matter if it was placed in-game or in editor).

    Has anyone encountered this or have any ideas what might be causing it?
     
  2. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    This is just return array full of nulls..
    availableLandTileMap.GetTilesBlock(buildingArea)
    If you need find your bug you should look in GetTilesBlock method.This is your own method or you using someones api?
     
  3. umbciok

    umbciok

    Joined:
    Nov 20, 2021
    Posts:
    4
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
  5. umbciok

    umbciok

    Joined:
    Nov 20, 2021
    Posts:
    4
    Yes, I agree that it does not seem like a bug, it's more likely to be something I missed when programming, but at this point (after a few hours trying to understand what is going on) I am not sure anymore.
    Here is a screenshot from Unity:


    The right tilemap is assigned to the script and also you can see, that this tilemap has tiles all over it.
    Debugger still shows that
     availableLandTileMap.GetTilesBlock(buildingArea); 
    returns a TileBase array (size 49, as expected) but filled with nulls.

    Is there something else that I am missing?
     
  6. umbciok

    umbciok

    Joined:
    Nov 20, 2021
    Posts:
    4
    I found the root of the problem.

    This line was the perpetrator:
     temp.unlockedArea.position = gridLayout.WorldToCell(temp.gameObject.transform.position) + new Vector3Int(-25, -25, 1); 


    unlockedArea was simply on a different Z coordinate compared to the rest. It should have been:
     temp.unlockedArea.position = gridLayout.WorldToCell(temp.gameObject.transform.position) + new Vector3Int(-25, -25, 0); 


    Thanks to MartinMa_BStudio for encouraging me to look for the mistake I made, instead of blaming it on Unity.