Making a Tile Game in Unity from Scratch
Objective: Make a Tile game that adds or subtracts points.
First thing that needs to be done is to create the background. Create an Image and size it how you would like.
We can also create an empty object and add a Grid layout Group component. Since this will hold 15 items and be a max of 3 rows, we will set the constraint to Fix Row Count and add in 3. Spacing can be adjusted to what looks good.
Now will be time to create the tiles. In this game we will have 3 different types of items. All will be created in a similar way just the text will say something different and a different icon. Create a Empty object. Under the Empty object create an Image that will be the background.
Next create another image that will be the icon.
Next is the text. Create a text object and place it under the icon. I set the text size to be best fit to help it look good at the size it is.
Last thing to is to duplicate the BG image and make it darker to make it look not selected. This will be a cover.
For the Cover image, make sure it is the last child to cover all previous items. This is what the Hierarchy should look like for these objects.
To make them look blue as shown above, drop them into the project window to make them prefabs.
Duplicate the items in scene while they are children to the grid to adjust the position and spacing better.
Now to add in two text objects, one for Score Title and another to show the score.
With the scene set up, we now need to focus on how to instantiate the items into the scene. Create a new C# script, this script will instantiate 15 objects as children to the grid object. The script will need a Game Object Array variable.
In Start create a for loop that will stop when i hit’s 15. In this For loop. There will be a temp variable that gets a random number between 0 and 2. Next line will Cast the instantiated object to become a game object. Last thing in the loop will all the instantiated items as a child to the grid.
It is possible to drop this script on any object. Since the grid will hold these items I drop the script onto it. I also added the three prefab items we created earlier into the array.
Create another script that will hold the score. This script will need the TMPro library. It will also need a Text variable and an int variable. After that create a public method that will pass in a int. Set the _score int to increment addPoints int. Last thing for this script will be to set the text.
In Unity create a Empty Object named Manager and add the GameManagerButton script onto it. We can drop the scoreamount text we created earlier into it.
One more script to handle the tiles behavior. This script will need the UnityEngine.UI library. It will also need three variables, one for the tiles value(this variable affects the score), the GameManagerButton reference, and lastly a reference to the cover image.
In Start we will assign the GameManagerButton varaible to the script by finding the object that has the script on it.
Create a public method. This method will call the public method in GameManagerButton script and send the value of this tile. Last thing would be to disable the tile cover by setting it to false.
Add this script onto each of the Items prefab. You can double click on the prefab to open Prefab mode. Here it will be easier to assign the cover to it’s reference and set the value of the item.
The last thing that needs to be done to make the tile clickable would be to make the tile a button. Before closing the prefab mode, select the cover image and add the button component.
In Onclick(), add a new event and drop in the Parent Object of the cover and select public method you created.
Make sure to do this to all three items. And with that we have created this tile game!
Thanks for reading! Have a wonderful day!