Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SetTilesBlock sometimes fails assertion and doesn't work as expected

Discussion in '2D Experimental Preview' started by robocup30, Jul 13, 2017.

  1. robocup30

    robocup30

    Joined:
    May 2, 2017
    Posts:
    7
    When I use the function SetTilesBlock to copy tiles of a tilemap to another tilemap I sometimes get an assertion error while it shouldn't.

    Assertion failed: The number of tiles set is not equal to the count from the dimensions given.
    UnityEngine.Tilemaps.Tilemap:SetTilesBlock(BoundsInt, TileBase[])

    Interestingly, the call only fails when I try to do it the first time after loading up Unity and after failing assertion once, the function will work as expected. I've also noticed that if I open up Tile Palette window and select brush tool first and then try calling SetTilesBlock, the assertion error will not happen.

    My code is this
    Code (CSharp):
    1.     public void AddTiles()
    2.     {
    3.         tilemap = GetComponent<Tilemap>();
    4.         Debug.Log("ADDING TILES");
    5.  
    6.         BoundsInt bound = new BoundsInt(0, 0, 0, 10, 10, 1);
    7.         TileBase[] tiles = tilemap.GetTilesBlock(bound);
    8.  
    9.         targetTilemap.SetTilesBlock(bound, tiles);
    10.     }
    Where I'm basically trying to copy tiles from tilemap and copy them on to targetTilemap.
    Which I'm calling through custom inspector button I added.

    Example project can be downloaded here
    To recreate the bug in the example project:
    1. Open Main scene
    2. View Room->Tilemap 1 Gameobject on inspector
    3. Click Add button on CompositeTileMap script on inspector
    4. When done first time there will be assertion error
    5. When clicking add again for second time, the error will correct itself

    I'm using Unity 2017.2.0b2 64bit