'grab grid tiles in unity

First of all: I'm quite new to unity and c#, so please be nice if I ask dumb questions.

I'm trying to make a game like minesweeper. So I want an easy grid with covered tiles and if you klick on one, it opens up.

I use a main script I create a grid like so:

private void CreateTieleSet(int width, int height){
    _tiles = new Dictionary<Vector2, Tile>();
    for (int x = 0; x < width; x++) 
    {
        for (int y = 0; y < height; y++) 
        {
            var spawnedTile = Instantiate(_tilePrefab, new Vector3(x, y), Quaternion.identity);
            spawnedTile.name = $"Tile {x} {y}";
            spawnedTile.status = field[x, y];

            _tiles[new Vector2(x, y)] = spawnedTile;
        }
    }
}

I use the function

public Tile GetTileAtPosition(Vector2 pos)
{
    if(_tiles.TryGetValue(pos, out var tile))
    {
        return tile;
    }
    return null;
}

to get the tile at the position xy, but I can't use it since

Tile tempTile = GetTileAtPosition(new Vector2(x, y));
tempTile.ChangeSprite(field[x, y]);    //example-funktion from tile-script

allways results in NullReferenceExeption-Error. I know the probleme, since I allways struggle with using scripts from other tiles. Bus usually I can use [SerializeField] Tile... and than dragdrop it onto it. In this case however I obvioulsy can't do that.

Btw: I realy tried to solve this problem with other solutions found online, but everyone has complete different ideas how to do it and nothing seems to work for me :/



Solution 1:[1]

Your best bet for this one is going to be using Unity's built-in Tilemaps - you'll want to take your sprites for Minesweeper and add them to a TilePalette, and paint them onto a GameObject with a Grid component, with children GameObjects holding a Tilemap component - one per layer. Then, when you want to change the sprite, you can do something like the following:

using UnityEngine;
using UnityEngine.Tilemaps;

class GridManager 
{
     [SerializeField] Tilemap gridDisplayLayer;

     public void ChangeSpriteAtLocation(Vector3Int cellLocation, Tile tileToChangeTo) 
     {
          gridDisplayLayer.SetTile(cellPosition, tile);
     }
}

If you want the simplest answer, just add Tile members to the class above for swapping between them and call ChangeSpriteAtLocation during runtime. Might need to change the flags on the Tilemap to change the sprite and such, but I think that should be good enough to get you started :) When you generate the TilePalette, Unity will give you Tile assets to save that you can then reference for the above example method.

I'm still getting all the intricacies of Tilemaps myself, though, and I'm definitely more design minded, so I won't be surprised if someone has a more succinct or efficient solution.

Hope this helps get you started, though!

  • Vellv

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vellv