Join Kelley Hecker for an in-depth discussion in this video Script a basic game piece, part of Build a Match 3 Game with Unity.
- [Voiceover] Next we'll create our normal game piece. This piece can come in six different colors and when three or more of the same color are in a row, they'll disappear. We'll worry about matching and clearing pieces later. For now we'll define only the most basic functions of the game piece. In the Scripts folder right click and from the menu select Create, C sharp script and name it GamePiece. Click on the PreFabs folder and select the NormalPiecePrefab. Go back to the Scripts folder and drag the game piece component over to the inspector to add it to our prefab.
Now go ahead and open the game piece script. To start with, we know that each game piece will have an X and Y coordinate on the grid, so we'll add those first. I've created two private ints, x and y, and then public properties for them. Notice that we only provide a getter for the position. This is because not all pieces will be able to move, so not all pieces should allow their position to be changed. In just a moment we'll create a moveable piece. We also know that each piece will have a type.
Right now we only have the normal piece type, but later we'll have other types. Like I did with the position I created a private type variable and then a public property to access that variable. I didn't add a setter because we don't want other classes changing our piece type. The game piece should also have a reference to our grid class in case it needs information about the grid or about other pieces on the board. Like I did before, I created a private variable and a public accessor. I called it GridRef because grid with a capital G was already taken by our class.
Finally we'll create an initialization function called Init. We'll call this function after we instantiate the game piece's game object so that we can initialize some of these variables. The Init function takes in the X and Y positions, the grid, and the piece type, which are all the parameters we defined earlier. I use an underscore just to differentiate between the parameters passed into the function and the parameters for our piece. Since we're inside the GamePiece class we can set these parameters directly since we don't have a setter.
Open up the Grid class and let's modify how we create the pieces to use this new GamePiece class. Instead of storing the game object for each piece we'll store the GamePiece component. All I'm doing now is switching our array from game objects to game pieces. I'll store the game object in a temporary variable called NewPiece. Since we're not storing the game object in the Pieces array anymore, I'll replace all the references to it with NewPiece.
And now we store the GamePiece component of the NewPiece game object in our Pieces array. And finally we call the Init function that we just wrote. I am passing in the X and Y coordinate, this, which is a reference to this grid class, and the piece type, which will just be Normal for now. It accidentally auto-completed these brackets so I'm just going to erase those. If you hit play you can see that not much has changed, but on the back end we're now using a custom game piece class instead of storing the game object.
Next we're going to make our pieces move.
Game developer Kelley Hecker covers topics like creating a grid-based game board, using inheritance to create different types of game pieces, adding obstacles and new levels, detecting matches and clearing pieces, and implementing a user interface complete with a score screen. By the end of the course, members will have a completed game and learned new techniques to apply to their next Unity project.
- Creating a game board grid
- Creating and scripting game pieces
- Filling the board
- Creating obstacles
- Swapping pieces
- Matching pieces
- Clearing obstacles and pieces
- Creating new levels
- Creating the user interface: HUD, game over screen, etc.