To be able to use your TurretSpanwer
in any scene, you can turn it into a prefab.
TurretSpawner
into a prefabTile
prefabs to the Tile
layer. Tile
layerTileCursor
and a TurretSpawner
to your sceneTileCursor
and TurretSpawner
to listen to the tiles in your level's gridYou may have noticed that you are unable to place turrets within another turrets Area of Engagement. Recall that you fixed this for your Test Scene in 15 - Turret Spawner > Camera Event Layer.
Tile
layerGameCamera
PrefabAs you continue to add new scenes to your game, you may need to adjust the camera again. Ideally, the camera will work the same in every scene. To help create this consistency, you can create a GameCamera
prefab that can be reused in your test scenes. Additionally, it can be useful to have the camera nested in a parent object just in case you need to add sibling game objects.
Main Camera
and name it GameCameraNow that you have a way to spawn turrets into your scene during Play Mode, you can remove the starting turrets from the scene.
Next, you will add a button to your scene that enables the TileCursor
and TurretSpawner
GameObjects when clicked.
Verify that disabilng TileCursor and TurretSpawner disables the ability to place turrets.
TileCursor
and TurretSpawner
in the HierarchyTileCursor
and TurretSpawner
+
iconUI
> Button - TextMeshPro
The first time you add a TextMeshPro
object to a Unity Project, you will be prompted to import the Text Mesh Pro library.
After the library has been imported, you should see 3 new Game Objects in your Hierarchy
You may be wondering what are the Canvas and EventSystem objects that were added to your scene. The Canvas in Unity is a component that acts as the root for all UI elements, determining how they are rendered and positioned in the game world or screen space. The EventSystem is responsible for processing and managing user input, such as mouse clicks, touches, and controller interactions, ensuring that UI elements respond correctly. Together, the Canvas and EventSystem enable interactive user interfaces by handling rendering and input events within Unity’s UI system.
If you are in the Scene tab, you probably do not see the Button you added. This is because the button is added to the Canvas which, by default, exists as a screen overlay.
Unfortunately, setting the position of the button in this way will only work if your player is using the exact same resolution as you. Unity provides tools for testing different resolutions so you can determine how your game will work in different screen modes.
This is happening because your button was positioned relative to the center of the screen. Thus, if the user is using a smaller resolution, the button will appear closer to the center of the screen. To help with this, Unity provides a way to specify a Pivot point.
By default, Unity's UI button object comes with an OnClick Listener that can be used in the Inspector to invoke a method on an object.
Select your button in the Hierarchy
In the Inspector find the Button
component
Within the Button
component, locate the On Click
event
Add a Listener by clicking the +
icon
Add the TileCursor
to the listener
Invoke the GameObject.SetActive
method and set it to true (enable checkbox)
Enter Play Mode
Verify that the cursor does not display if you move on a tile
Click the Button
Verify that the cursor does display if you move on a tile
When you're finished, your game should operate similar to the video below
Now that you have a basic understanding about adding a button to your scene that can invoke methods on your other GameObjects, it is time to add in resources that will determine if the player is able to place a turret on the map.