0
\$\begingroup\$

I'm trying to get a 2 dimensional grid of values from a tilemap and found this which seems to do the trick:

_tileMap = gameObject.GetComponent<Tilemap>();

foreach (var position in _tileMap.cellBounds.allPositionsWithin)
{
    Debug.Log(gameObject.name + ": " + position);
    // TileBase tile = _tileMap.GetTile(position);
    // if(tile.name != null) Debug.Log(tile.name);
}

However, something strange happens as it produces two values, when I only have one tile on the tilemap and when I uncomment my .name log above it only works for the second value and errors for the first.

Any help of the best way of finding values from tilemaps would be great.

\$\endgroup\$
4
  • \$\begingroup\$ No, Tilemap is a class in unity as of any of the unity 2018 builds \$\endgroup\$
    – benbyford
    Commented May 22, 2018 at 9:18
  • \$\begingroup\$ docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.html \$\endgroup\$
    – benbyford
    Commented May 22, 2018 at 14:27
  • 1
    \$\begingroup\$ Prefect. The search wasn't bringing it up. Thanks. *reviews docs* \$\endgroup\$ Commented May 22, 2018 at 15:18
  • \$\begingroup\$ Hum. allPositionsWithin seems to imply that if you have a sprite that's larger than "1x1" this will return all of the coordinates contained within (for a 1x1 it would return only its actual position). There aren't necessarily other sprites AT those locations. But you're having the problem even at a minimal size...there's clearly something I don't understand, then. \$\endgroup\$ Commented May 22, 2018 at 17:16

0

You must log in to answer this question.

Browse other questions tagged .