0
\$\begingroup\$

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:

public class GameLevelView : MonoBehaviour
{
    private Tilemap _grid;
    private Tile[] _tiles;
    public MapData Map { get; set; } //supplied in code by an external source

    void Start()
    {
        _grid = GetComponent<Tilemap>();
        _tiles = Resources.LoadAll<Sprite>("tiles").Select(s => new Tile { sprite = s }).ToArray();
    }

    void Update()
    {
        Map.PaintTo(_grid, _tiles);
    }
}

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Found the problem. For whatever reason, Unity was creating the Tilemap with a Lit material by default, and I didn't have any 2D lights in my scene. I can't help but wonder when this changed; I've done tilemap-based stuff before and never had to care about lighting.

\$\endgroup\$
1
  • \$\begingroup\$ I think it changed when separate 2d lighting was added. \$\endgroup\$
    – Nikaas
    Feb 1, 2022 at 6:45

You must log in to answer this question.

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