Search Unity

Question Tilemap is rendering all tiles as black squares

Discussion in '2D' started by MasonWheeler, Jan 31, 2022.

  1. MasonWheeler

    MasonWheeler

    Joined:
    Apr 2, 2016
    Posts:
    219
    I'm trying to render a 2D tile map. I've placed a Tilemap object in the scene from the create 2D object context menu, set a reference to it to one of my script objects, and tried to render. It's definitely placing tiles, (I can see it go from transparent to drawing something,) but instead of the tiles I'm giving it, it creates a pure black square in every cell.

    My tiles are coming from a tileset image, comprised of a 16x16 grid of tiles of 64x64 pixels in size. The tile setup code is as follows:

    Code (CSharp):
    1. public class GameLevelView : MonoBehaviour
    2. {
    3.     private Tilemap _grid;
    4.     private Tile[] _tiles;
    5.     public MapData Map { get; set; } //supplied in code by an external source
    6.  
    7.     void Start()
    8.     {
    9.         _grid = GetComponent<Tilemap>();
    10.         _tiles = Resources.LoadAll<Sprite>("tiles").Select(s => new Tile { sprite = s }).ToArray();
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         Map.PaintTo(_grid, _tiles);
    16.     }
    17. }
    The PaintTo routine simply loops over every cell, gets a tile index, and calls
    tilemap.SetTile(new Vector3Int(x, y, 0), tiles[idx]);
    . This seems simple enough that nothing could go wrong... and yet all the tiles are rendering black.

    In case it's relevant, using Unity 2020.3.26f1 with the 2D URP. Things I've already verified:
    • The tileset image has been imported as a sprite sheet correctly.
    • The tile array loading code is working as expected.
    • The tiles produced by the tile array have their values set up as expected. For example, their Color property is
      (1,1,1,1)
      rather than somehow being black.
    • The GameObject holding the tile map is active in the scene and positioned correctly on the camera.
    • MapData is being set up correctly.
    At this point I have no idea what else to look at to try and make this work. Has anyone seen this before and might know how to fix this?
     
  2. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    249
    It's possible that the sprite assets loaded by Resources.LoadAll are getting unloaded. Perhaps somewhere else that isn't even in your own code. That might explain your colorless sprites. If you look at a position in the actual tilemap at runtime by using in the tile palette's selector (press S) you can see (via the selection inspector) what's actually in that position.

    Given your list of what you've checked, you omitted "checked to see if there's really a sprite in there" so you could look in the _tiles array and see if the tiles actually have any sprites after you've gone thru your update loop.

    BTW, Do you really want to repaint the entire map every frame since that's what would happen since Update runs every frame.

    Hope this helps...
     
  3. MasonWheeler

    MasonWheeler

    Joined:
    Apr 2, 2016
    Posts:
    219
    upload_2022-1-31_11-20-11.png

    The sprites are definitely being loaded into the tiles.

    Checked that by making it public and inspecting the tiles at runtime. Again, seeing the same result. All of the tiles are loaded with their sprites, exactly as expected.

    At the moment I don't care about that; I just want to get it to paint. Once that happens, I'll worry about performance.
     
  4. ashlit1998

    ashlit1998

    Joined:
    Jan 25, 2019
    Posts:
    10
    I know this is a 2d project, but could it have anything to do with lighting? What happens if you paint a part of the map before running the game? Could the update function's redrawing be causing problems for you? Maybe run it on Input.MouseDown(0) or something. Keep us updated!
     
    unity_23aferg8643 likes this.
  5. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    249
    You might try moving the stuff from Update right into your Start method. Repainting the tiles repeatedly might be part of the issue. Every time that you paint a tile a lot of 'stuff' happens and not necessarily immediately.
     
  6. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    Is your TilemapRenderer using the Sprite Lit material? And do you then also have a Global Light 2D?
     
    Georg-Axelsson and jpbracaprado like this.
  7. MasonWheeler

    MasonWheeler

    Joined:
    Apr 2, 2016
    Posts:
    219
    Ugh, yeah, that was the problem. I didn't even think about lighting. Thanks!
     
  8. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    249
    good catch!
     
    jpbracaprado likes this.
  9. zombee81

    zombee81

    Joined:
    May 15, 2013
    Posts:
    11
    The problem was the Target Sorting Layers in the Global Light 2D, because you can restrict which layers receive the light, but by default it only assigns the default layer. So if you create more layers you must remember to add them to the Target Sorting Layers.
     
    granit and Texugo23 like this.
  10. marcelbzrra

    marcelbzrra

    Joined:
    May 21, 2018
    Posts:
    1
    The same issue happened to me.
    But I realized that I changed the tile color accidentally to black :D
    upload_2023-1-6_16-43-15.png