Newbie here, please help on a match-3 ish game

Firstly, hello to all of you,

I’ve always wanted to be a game maker and this week I made a start with Unity and C#. So I am new to Unity and C#. I’ve done the 2D rougelike game from the tutorials and now I am trying to make a game like a regular match 3. My game can generate a random grid with 6 different colors and I can destroy the tile on click and the tiles above the destroyed one fills it place then my game generates a random tile to fill the empty top.

But, what I want from my game is to destroy all adjacent tiles that has the same color with the clicked one. I know I need to write a script that checks the surroundings of the clicked and if there is a tile with the same color it needs to check the surroundings of the founded tile and so on… I don’t know where to start and how can I learn it. So do anyone know where can I learn it or how can I do it? Thanks in advance. :slight_smile:

Hey Rodopeace, welcome to game development :slight_smile:

There are many different ways you could approach something like this, but one thing that could help is learning about Raycasting.

Since you have a static grid, you can shoot a ray in every adjacent block to get info back about the objects that are in them. Once you have that, you run your comparisons that then do what you need to do.

Like I said though, there are many ways to come at this problem.

Match 3 games should usually be made where a 2D array represents the game world, so you can just check positions in the array. This keeps things 100% deterministic and easier to manage. You still use GameObjects to display the tiles.

–Eric

3 Likes

Thank you very much for showing me a way to improve. I saw the array based match 3 games and how they worked. Since my project was simple I thought I don’t need to do that. Now I am watching Raycasting videos to learn the concept as SarfaraazAlladin suggested. If I can’t manage it only with that then I will recreate it with arrays.

Maybe I should recreate them in arrays since the code looks a little weird. :slight_smile: Thank you guys again.

for (int rowNum = -3; rowNum < 6; rowNum++)
        {
            for (int colNum = -4; colNum < 5; colNum++)
            {
                whichGem = Random.Range(1, 7);
                if (whichGem == 1)
                {                   
                Instantiate(redObj, new Vector2(colNum, rowNum), redObj.rotation);
                        }
                if (whichGem == 2)
                {
                    Instantiate(blueObj, new Vector2(colNum, rowNum), blueObj.rotation);                             }
                if (whichGem == 3)
                {
                    Instantiate(pinkObj, new Vector2(colNum, rowNum), pinkObj.rotation);
                               }
                if (whichGem == 4)
                {
                    Instantiate(greenObj, new Vector2(colNum, rowNum), greenObj.rotation);
                               }
                if (whichGem == 5)
                {
                    Instantiate(purpleObj, new Vector2(colNum, rowNum), purpleObj.rotation);
                               }
                if (whichGem > 5)
                {
                    Instantiate(yellowObj, new Vector2(colNum, rowNum), yellowObj.rotation);
                               }
            }
        }

Note: I had to add the .rotation because code did not work.

Working with arrays is a much simpler approach, even taking into account that Unity takes care of most of the hard stuff in raycasting code.