{"id":79183,"date":"2018-06-07T13:24:00","date_gmt":"2018-06-07T13:24:00","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=79183"},"modified":"2021-07-29T19:44:13","modified_gmt":"2021-07-29T19:44:13","slug":"using-unity-ui-and-c-to-create-a-tic-tac-toe-game","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/using-unity-ui-and-c-to-create-a-tic-tac-toe-game\/","title":{"rendered":"Using Unity UI and C# to Create a Tic-Tac-Toe Game"},"content":{"rendered":"<h4>The series so far:<\/h4>\n<ol>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/introduction-game-development-unity-c\/\">Introduction to Game Development with Unity and C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/using-c-functions-animations\/\">Using C# Functions in Your Animations<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/procedural-generation-unity-c\/\">Procedural Generation with Unity and C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/pathfinding-unity-c\/\">Pathfinding with Unity and C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/extending-unity-editor-c\/\">Extending the Unity Editor with C#<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/c-programming\/using-unity-ui-and-c-to-create-a-tic-tac-toe-game\/\">Using Unity UI and C# to Create a Tic-Tac-Toe Game<\/a><\/li>\n<\/ol>\n\n<p>Creating a user interface (UI) is undeniably important for your video game projects. The UI is responsible for displaying information to the player like health, ammo, and score. Without easy access to that information, games can fall apart very quickly. Just as important as displaying that info is updating it as the player plays. If the player earns ten points, then the UI needs to reflect that and update the score currently present on the screen.<\/p>\n<p>Here, you&#8217;ll be using Unity&#8217;s UI in a different way. Rather than display health and score, you will instead utilize the UI to create a playable game of Tic-Tac-Toe (also sometimes called noughts and crosses or Xs and Os), and of course you will use C# code to do it. The aim of this project is to demonstrate how, through code, you can manipulate UI elements to do whatever you desire. A grid will be layed out for the game, and from there buttons will be added to the grid. These buttons will act as the playable spaces for the Tic-Tac-Toe game.<\/p>\n<h2>Setting Up<\/h2>\n<p>As with all projects, the first thing you&#8217;ll have to do is create a new project. Click the <em>New<\/em> button at the top of Unity&#8217;s project management window as shown in Figure 1.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"128\" class=\"wp-image-79184\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 1: Creating a new project.<\/p>\n<p>Name your project <em>TicTacToe <\/em>and be sure to set it as a 2D project. This project can just as easily work as a 3D project, but this example will be in 2D for the entire duration. After you&#8217;ve done this, set a path for your project and click <em>Create project<\/em> when you&#8217;re ready. Figure 2 shows the final project setup screen before clicking <em>Create project<\/em>.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"580\" class=\"wp-image-79185\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-1.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 2: Project setup screen.<\/p>\n<p>Once Unity has created the project, the very first thing you need to do is go to the <em>Hierarchy <\/em>window and select <em>Create-&gt;UI-&gt;Panel <\/em>to create a new panel. While you&#8217;re at it, double click the new <em>Panel <\/em>object to center the scene view on the panel. Figure 3 shows how to create a new panel.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"365\" height=\"584\" class=\"wp-image-79186\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-2.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 3: Creating a new UI panel.<\/p>\n<p>Go ahead and name this object <em>line <\/em>then select the box to the far left of the <em>Inspector <\/em>window as shown in Figure 4.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"569\" height=\"261\" class=\"wp-image-79187\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-3.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 4: Naming the object and selecting the anchor selection box.<\/p>\n<p>This is the object&#8217;s &#8216;anchor,&#8217; which is responsible for keeping the UI element at a certain point on the screen regardless of the size of the user&#8217;s screen. It&#8217;s useful whenever you need to make sure the UI will look the same from one screen to the next. In this case, the <em>line <\/em>object is set to stretch both horizontally and vertically to the ends of the screen, but that&#8217;s not very useful for a game of Tic-Tac-Toe. Select the center anchor option to anchor the <em>line <\/em>object to its relative center as seen in Figure 5.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"519\" height=\"516\" class=\"wp-image-79188\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-4.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 5: Setting an object&#8217;s anchor to center.<\/p>\n<p>Next, the size of the <em>line <\/em>object needs to be set. Under the <em>Inspector <\/em>window in the <em>Rect Transform <\/em>component set the object&#8217;s <em>Pos Y <\/em>value to -5, its <em>Width <\/em>to 18, and its <em>Height <\/em>to 274. Figure 6 shows the different fields to change and their values.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"586\" height=\"203\" class=\"wp-image-79189\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-5.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 6: Setting the <em>Pos Y, Width, and Height <\/em>of the line object.<\/p>\n<p>Duplicate the object three times to bring you to a total of four <em>line <\/em>objects. Once you have these objects created, you&#8217;ll need to adjust the position and sizes of each <em>line <\/em>object using its <em>Rect Transform <\/em>component to make a grid to play in. Table 1 shows each <em>line <\/em>object&#8217;s properties to set and what value to set them to.<\/p>\n<p>ObjectPos XPos YWidthHeight<\/p>\n<table class=\"table--tight\">\n<tbody>\n<tr>\n<th>\n<p>line<\/p>\n<\/th>\n<td>\n<p>0<\/p>\n<\/td>\n<td>\n<p>-5<\/p>\n<\/td>\n<td>\n<p>18<\/p>\n<\/td>\n<td>\n<p>274<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>line (1)<\/p>\n<\/th>\n<td>\n<p>110<\/p>\n<\/td>\n<td>\n<p>-5<\/p>\n<\/td>\n<td>\n<p>18<\/p>\n<\/td>\n<td>\n<p>274<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>line (2)<\/p>\n<\/th>\n<td>\n<p>56<\/p>\n<\/td>\n<td>\n<p>52.5<\/p>\n<\/td>\n<td>\n<p>296<\/p>\n<\/td>\n<td>\n<p>19<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>line (3)<\/p>\n<\/th>\n<td>\n<p>56<\/p>\n<\/td>\n<td>\n<p>-52.5<\/p>\n<\/td>\n<td>\n<p>296<\/p>\n<\/td>\n<td>\n<p>19<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"caption\">Table 1: The line objects size and coordinates.<\/p>\n<p>With the grid created, create a UI button by using the <em>Create <\/em>menu from before, selecting <em>UI <\/em>and then <em>Button<\/em>. Figure 7 shows you where to find this option.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"380\" height=\"560\" class=\"wp-image-79190\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-6.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 7: Creating a new button.<\/p>\n<p>Name this button <em>Space<\/em> and then click the arrow next to the name of the object in the <em>Hierarchy<\/em> like in Figure 8.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"102\" height=\"135\" class=\"wp-image-79191\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-7.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 8: Expanding the Space object to reveal its child Text object.<\/p>\n<p>This will reveal the <em>Text <\/em>object attached to the button. Select <em>Text <\/em>and then find the <em>Text <\/em>component under the <em>Inspector <\/em>window. Once found, clear all text in the <em>Text <\/em>field so that no text appears on the newly created button. Figure 9 shows where to find this.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"342\" height=\"335\" class=\"wp-image-79192\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-8.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 9: The <em>Text <\/em>field.<\/p>\n<p>From there set the font size to 36 and make sure its alignment is set exactly in the center of the button like in Figure 10.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"561\" height=\"354\" class=\"wp-image-79193\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-9.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 10: Setting different properties of the <em>Text <\/em>component.<\/p>\n<p>With the <em>Text <\/em>object done, select the <em>Space <\/em>object from the <em>Hierarchy <\/em>and go to the <em>Rect Transform <\/em>component. Set the <em>Pos X<\/em> value to -50, <em>Pos Y <\/em>to 100, <em>Width <\/em>to 75, and <em>Height <\/em>to 70. The object&#8217;s <em>Rect Transform <\/em>should now look like the one shown in Figure 11.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"576\" height=\"263\" class=\"wp-image-79194\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-10.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 11: Space&#8217;s <em>Rect Transform <\/em>properties.<\/p>\n<p>And your button should now also be set in the upper left corner of your grid like in Figure 12.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"289\" height=\"279\" class=\"wp-image-79195\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-11.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 12: The scene so far.<\/p>\n<p>Now is not quite the time to lay out the rest of the buttons, as there are things you&#8217;ll need to do first that will ultimately make development easier for you. You will come back to laying out the different spaces on the grid after the code is complete. For now, create a new panel using the same <em>Create <\/em>menu as before. Name this panel <em>GameOver Panel <\/em>and set its anchor to the center of the screen. Once that&#8217;s done, adjust its size and position according to Figure 13.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"583\" height=\"279\" class=\"wp-image-79196\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-12.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 13: GameOver Panel&#8217;s <em>Rect Transform <\/em>properties.<\/p>\n<p>After that&#8217;s done, you need to change its color a bit to help make it stand out from all the rest of the UI. Under the <em>Image <\/em>component select <em>Color <\/em>like in Figure 14.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"583\" height=\"225\" class=\"wp-image-79197\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-13.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 14: Setting a new color for the GameOver Panel<\/p>\n<p>In Figure 15 a green color was chosen, but of course you may use whichever color you want. Keep in mind that if you use black as the color, you&#8217;ll need to later change the color of a text object you&#8217;ll be creating in a moment so that said text is actually visible.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"489\" height=\"474\" class=\"wp-image-79198\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-14.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 15: Picking a color.<\/p>\n<p>Once your color is chosen, right click on the <em>GameOver Panel <\/em>object and select <em>UI-&gt;Text <\/em>to create a new <em>Text <\/em>object. Since you created the <em>Text <\/em>object in this way Unity will automatically assign the new object as a child of <em>GameOver Panel<\/em>, which is exactly what is needed. Figure 16 shows where to find the option to create the <em>Text <\/em>object.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"498\" height=\"794\" class=\"wp-image-79199\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-15.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 16: Creating a new <em>Text <\/em>object as a child of GameOver Panel.<\/p>\n<p>With the new <em>Text <\/em>object selected, change the following values in the <em>Rect Transform <\/em>and <em>Text <\/em>components.<\/p>\n<ul>\n<li><em>Rect Transform-&gt;Pos Y: <\/em>-3<\/li>\n<li><em>Rect Transform-&gt;Width: <\/em>295.5<\/li>\n<li><em>Rect Transform-&gt;Height:<\/em> 68<\/li>\n<li><em>Text-&gt;Text:<\/em> Victory Text<\/li>\n<li><em>Text-&gt;Font Size:<\/em> 36<\/li>\n<li><em>Text-&gt;Alignment:<\/em> Center<\/li>\n<\/ul>\n<p>When you&#8217;re done the <em>Text <\/em>object should look like the one in Figure 17.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"573\" height=\"671\" class=\"wp-image-79200\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-16.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 17: The finished Text object.<\/p>\n<p>Next another button will be made. This button will restart the game whenever it is clicked on, but you&#8217;ll set it up so it will only appear once the game is done, whether there&#8217;s a winner or if it ends in a tie. Using the same <em>Create <\/em>menu as before, create a new button and call it <em>RestartGame<\/em>. Then, set the following <em>Rect Transform <\/em>values:<\/p>\n<ul>\n<li><em>Pos X: <\/em>-250<\/li>\n<li><em>Pos Y:<\/em> 70<\/li>\n<li><em>Width:<\/em> 200<\/li>\n<li><em>Height:<\/em> 70<\/li>\n<\/ul>\n<p>After that, select <em>RestartGame&#8217;s Text <\/em>object and set the following properties in the <em>Text <\/em>component:<\/p>\n<ul>\n<li><em>Text: <\/em>Play again?<\/li>\n<li><em>Font Size:<\/em> 24<\/li>\n<li><em>Alignment:<\/em> Center<\/li>\n<\/ul>\n<p>Finally, create a new empty object by going to <em>Create-&gt;Create Empty<\/em> like in Figure 18.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"307\" height=\"290\" class=\"wp-image-79201\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-17.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 18: Creating the GameController object.<\/p>\n<p>Name this object <em>GameController<\/em>. This object will be the one that takes care of almost all the code for the game. Speaking of the code, it&#8217;s now time to create some scripts. In your <em>Assets <\/em>window right click and select <em>Create-&gt;C# Script <\/em>like in Figure 19.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"661\" class=\"wp-image-79202\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-18.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 19: Creating a new C# script.<\/p>\n<p>Name this script <em>GameController<\/em>. Remember that you must give it the <em>GameController <\/em>name immediately after creating the script. If you click away from the script without giving the script its name, the wrong class will be created in Visual Studio which can cause problems later. Immediately after creating that script, create another script and call it <em>Space<\/em>. Once this is done, your <em>Assets <\/em>window should look like the one in Figure 20.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"286\" height=\"178\" class=\"wp-image-79203\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-19.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 20: The current <em>Assets <\/em>window.<\/p>\n<p>It&#8217;s now time to write the code that will make this game run! Begin in the <em>GameController <\/em>script by double clicking it in the <em>Asset<\/em> window.<\/p>\n<h2>The Code<\/h2>\n<p>The <em>GameController <\/em>script will be responsible for the majority of the game&#8217;s logic. There&#8217;s a couple tasks that need to be done before creating this script. First, comment out or delete the <strong>Update<\/strong> function as it will be of no use to you in this project. Next, at the top of the script just underneath the line that says <strong>using UnityEngine<\/strong> add the following line:<\/p>\n<pre class=\"lang:c# theme:vs2012\">using UnityEngine.UI;<\/pre>\n<p>After you&#8217;ve added this line in, declare the following variables above the <strong>Start<\/strong> function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public Text[] spaceList;\r\npublic GameObject gameOverPanel;\r\npublic Text gameOverText;\r\npublic GameObject restartButton;\r\nprivate string side;\r\nprivate int moves;<\/pre>\n<p>How are these variables going to be used? The <em>spaceList <\/em>array at the top will hold all of the text that will be on our buttons in the game. You may be wondering why you wouldn&#8217;t have an array of buttons instead of text. This is because you want to check the text on the button when looking for a winner. There&#8217;s no information that the button object itself can directly tell you about as far as whether there&#8217;s an X or an O on the space. Thus, we want the text instead so you can check that information more directly.<\/p>\n<p>Also declared is the <em>gameOverPanel<\/em> which will simply be activated whenever the game has ended. You will also have the <em>gameOverText<\/em> to display whenever a game has been finished. The <em>restartButton <\/em>will appear at the end of a game to simply ask if you wish to start a new game of Tic-tac-toe. After that are the private variables, which will simply keep track of whose turn it is and how many moves have been played so far. At this point, the <em>GameController <\/em>script will look similar to the one in Figure 21.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"439\" height=\"457\" class=\"wp-image-79204\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-20.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 21: The GameController script so far.<\/p>\n<p>Now it&#8217;s time to move on to the <strong>Start<\/strong> function, where you will set a few variables and deactivate certain objects. This is the code needed for <em>Start<\/em>:<\/p>\n<pre class=\"lang:c# theme:vs2012\">SetGameControllerReferenceForButtons();\r\nside = \"X\";\r\ngameOverPanel.SetActive(false);\r\nmoves = 0;\r\nrestartButton.SetActive(false);<\/pre>\n<p>All you&#8217;re doing is setting the side to &#8216;X&#8217;, setting the number of moves to zero, and deactivating the <em>gameOverPanel <\/em>and <em>restartButton<\/em>. After all, you probably don&#8217;t want the <em>GameOver Panel<\/em> object getting in the way of the game, and it would be all too easy for a sore loser to restart the game before it&#8217;s finished. So, you deactivate these objects from the start. There&#8217;s also one function being called here that you have not yet created, but are about to. Figure 22 shows the finished <strong>Start<\/strong> function.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"401\" height=\"161\" class=\"wp-image-79205\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-21.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 22: The complete <em>Start <\/em>function for the GameController script.<\/p>\n<p>Underneath the commented out <strong>Update<\/strong> function (or just underneath the <strong>Start<\/strong> function if you chose to delete <strong>Update<\/strong> completely), create the <strong>SetGameControllerReferenceForButtons<\/strong> function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">void SetGameControllerReferenceForButtons()\r\n{\r\n\tfor (int i = 0; i &lt; spaceList.Length; i++)\r\n\t\tspaceList[i].GetComponentInParent&lt;Space&gt;().SetControllerReference(this);\r\n}<\/pre>\n<p>Once this function is made, the compiler error in the <strong>Start<\/strong> function should go away. But you&#8217;ve created a new one instead! On the line that says <strong>spaceList[i].GetComponentInParent&lt;Space&gt;().SetControllerReference(this);<\/strong> there&#8217;s a new error. The compiler doesn&#8217;t know what <strong>SetControllerReference<\/strong> is. You&#8217;re trying to get a function from a different script that, like <strong>SetGameControllerReferenceForButtons<\/strong>, has not yet been created. Figure 23 shows the completed function in <strong>GameController<\/strong><em>. <\/em><\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"132\" class=\"wp-image-79206\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-22.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 23: The SetGameControllerReferenceForButtons function.<\/p>\n<p>This is now a good time to switch over to the <em>Space <\/em>script. Odds are there will not be a tab at the top of Visual Studio that you can click to switch to the <em>Space <\/em>script. So instead, save your work and go back to Unity. In the <em>Assets <\/em>window double click the <em>Space <\/em>script you created earlier. Alternatively, you can also find the script in the Solution Explorer. Inside the <em>Space <\/em>script, you&#8217;ll need to begin similarly to the <em>GameController <\/em>script. Comment out or delete both the <strong>Start<\/strong> and <strong>Update<\/strong> functions, and be sure to add <strong>using UnityEngine.UI;<\/strong> near the top of the script like you did in <em>GameController<\/em>. Once you&#8217;ve done that, declare the following variables:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public Button button;\r\npublic Text buttonText;\r\nprivate GameController gameController;<\/pre>\n<p>When you&#8217;re finished the <em>Space <\/em>script should look like the one shown in Figure 24.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"385\" height=\"406\" class=\"wp-image-79207\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-23.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 24: The beginning of the Space script.<\/p>\n<p>This is now the point where you create the aformentioned <strong>SetControllerReference<\/strong> function. At the bottom of the script, add the following code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public void SetControllerReference(GameController control)\r\n{\r\n\tgameController = control;\r\n}<\/pre>\n<p>That&#8217;s all there is to this function. All it&#8217;s doing is getting a reference to the <em>GameController <\/em>script for later use. Note that it <strong><em>must <\/em><\/strong>be a public function for <em>GameController <\/em>to use this function. There&#8217;s not much else left to do with the <em>Space <\/em>script for the rest of the project, so you may as well finish it out since you&#8217;re here. Underneath <strong>SetControllerReference <\/strong>add another public function called <strong>SetSpace <\/strong>and give it the following code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public void SetSpace()\r\n{\r\n\tbuttonText.text = gameController.GetSide();\r\n\tbutton.interactable = false;\r\n\tgameController.EndTurn();\r\n}<\/pre>\n<p>The <em>GameController <\/em>script will be putting this function to use. After the player makes a move, this function will be called to place an X or an O on the space and make sure the button object you click is no longer interactable. It will then call <em>GameController&#8217;s <\/em><strong>EndTurn<\/strong> function to change whose turn it is. With this function finished, the <em>Space <\/em>script is finished. Figure 25 shows the finished script. Keep in mind that it&#8217;s okay for the errors in <strong>SetSpace<\/strong> to be there for now as you will be adding the needed functions momentarily to <em>GameController<\/em>, which will in turn remove those errors.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"534\" height=\"595\" class=\"wp-image-79208\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-24.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 25: The finished Space script.<\/p>\n<p>It&#8217;s now time to switch back over to the <em>GameController <\/em>script. You can either go back to Unity and double click the <em>GameController <\/em>script again or click the <em>GameController<\/em> tab near the top of your Visual Studio window like in Figure 26. Remember to save your work!<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"314\" height=\"121\" class=\"wp-image-79209\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-25.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 26: Switching to the GameController script.<\/p>\n<p>Once you&#8217;ve returned to the <em>GameController <\/em>script, go to the bottom of the script and add the following code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public string GetSide()\r\n{\r\n\treturn side;\r\n}<\/pre>\n<p>When the <em>Space <\/em>script calls this string function, all it\u2019s effectively doing is getting the current player&#8217;s turn and setting an X or O on whichever space you clicked on. Next it&#8217;s time to create the <strong>ChangeSide <\/strong>function, which will control the changing of player turns. The code for this is as follows:<\/p>\n<pre class=\"lang:c# theme:vs2012\">void ChangeSide()\r\n{\r\n\tif (side == \"X\")\r\n\t\tside = \"O\";\r\n\telse\r\n\t\tside = \"X\";\r\n}<\/pre>\n<p>After this is complete, your <em>GetSide <\/em>and <em>ChangeSide<\/em> functions should look similar to what&#8217;s shown in Figure 27.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"314\" height=\"223\" class=\"wp-image-79210\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-26.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 27: The <em>GetSide <\/em>and <em>ChangeSide <\/em>functions of GameController.<\/p>\n<p>Now it&#8217;s time to create perhaps one of the most important functions in the whole project. This function, called <strong>EndTurn<\/strong><em>, <\/em>will be handling the process of incrementing the <strong>moves<\/strong> variable as well as checking to see if any sort of game over state has been achieved. If a player has gained a victory, it will call a <strong>GameOver<\/strong> function which will be created momentarily. It will also check to see if the number of moves has reached a certain limit, and if it has, it will activate the <em>gameOverPanel <\/em>and <em>restartButton<\/em>. Here&#8217;s the code for the <strong>EndTurn<\/strong> function:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public void EndTurn()\r\n{\r\n        moves++;\r\n        if (spaceList[0].text == side &amp;&amp; spaceList[1].text == side &amp;&amp; spaceList[2].text == side)\r\n            GameOver();\r\n        else if (spaceList[3].text == side &amp;&amp; spaceList[4].text == side &amp;&amp; spaceList[5].text == side)\r\n            GameOver();\r\n        else if (spaceList[6].text == side &amp;&amp; spaceList[7].text == side &amp;&amp; spaceList[8].text == side)\r\n            GameOver();\r\n        else if (spaceList[0].text == side &amp;&amp; spaceList[3].text == side &amp;&amp; spaceList[6].text == side)\r\n            GameOver();\r\n        else if (spaceList[1].text == side &amp;&amp; spaceList[4].text == side &amp;&amp; spaceList[7].text == side)\r\n            GameOver();\r\n        else if (spaceList[2].text == side &amp;&amp; spaceList[5].text == side &amp;&amp; spaceList[8].text == side)\r\n            GameOver();\r\n        else if (spaceList[0].text == side &amp;&amp; spaceList[4].text == side &amp;&amp; spaceList[8].text == side)\r\n            GameOver();\r\n        else if (spaceList[2].text == side &amp;&amp; spaceList[4].text == side &amp;&amp; spaceList[6].text == side)\r\n            GameOver();\r\n        if (moves &gt;= 9)\r\n        {\r\n            gameOverPanel.SetActive(true);\r\n            gameOverText.text = \"Tie!\";\r\n            restartButton.SetActive(true);\r\n        }\r\n        ChangeSide();\r\n}<\/pre>\n<p>The way the <strong>EndTurn<\/strong> function checks for a victory is, admittedly, a little reliant on brute force. If, for example, you wanted to expand the board for any reason, then this solution would simply be no good and you&#8217;d have to either recode it to accomodate the different victory conditions or come up with an entirely different solution. For this project, the code above will work fine. All it&#8217;s doing is checking the text of each item in the <em>spaceList<\/em> array and seeing if there&#8217;s a three in a row combination. If there is, it calls <strong>GameOver <\/strong>and ends the game. In addition, if the number of moves played is equal to or greater than nine, it will also end the game.<\/p>\n<p>You&#8217;re not calling the <strong>GameOver function<\/strong> here, and you may be wondering why. It&#8217;s primarily because of the <strong>gameOverText.text = &#8216;Tie!&#8217;;<\/strong> line. In the <strong>GameOver<\/strong> function you&#8217;ll soon make, the game is just getting the current player&#8217;s side (X or O) and concatinating <strong>wins!<\/strong> to the text. In a draw, that wouldn&#8217;t make very much sense, and thus the process is handled a little differently. Finally, if none of the above game over conditions are met, then the game simply changes the side to the next player. Figure 28 shows the full <strong>EndTurn<\/strong> function.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"752\" height=\"518\" class=\"wp-image-79211\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-27.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 28: The <em>EndTurn <\/em>function in the GameController script.<\/p>\n<p>With <strong>EndTurn <\/strong>complete you will now need to create the <strong>GameOver<\/strong> function as follows:<\/p>\n<pre class=\"lang:c# theme:vs2012\">void GameOver()\r\n{\r\n        gameOverPanel.SetActive(true);\r\n        gameOverText.text = side + \" wins!\";\r\n        restartButton.SetActive(true);\r\n        for (int i = 0; i &lt; spaceList.Length; i++)\r\n            SetInteractable(false);\r\n}<\/pre>\n<p>This is similar to when you checked for how many moves had been played in the <strong>EndTurn<\/strong> function, but with a few extra steps. Immediately after this, create the <strong>SetInteractable <\/strong>function to remove the error in the <strong>GameOver<\/strong> function. Then, enter the following code:<\/p>\n<pre class=\"lang:c# theme:vs2012\">void SetInteractable(bool setting)\r\n{\r\n        for (int i = 0; i &lt; spaceList.Length; i++)\r\n            spaceList[i].GetComponentInParent&lt;Button&gt;().interactable = setting;\r\n}<\/pre>\n<p>All this code is doing is gathering all the <strong>Button<\/strong> components in the parent object of each text object in <strong>spaceList<\/strong> and setting interactable to whatever you assign in the <strong>setting<\/strong> boolean. Once this is finished, your <strong>GameOver<\/strong> and <strong>SetInteractable<\/strong> code should look similar to Figure 29.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"605\" height=\"278\" class=\"wp-image-79212\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-28.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 29: The <em>GameOver <\/em>and <em>SetInteractable <\/em>functions in GameController.<\/p>\n<p>At this point, you may think the coding part of this project is done, but there is actually one last function that needs to be made. After a game over state is achieved, the user will need some way to restart the game. To that end, you will need to make the <strong>Restart<\/strong> function. It&#8217;s very similar to the <strong>GameOver<\/strong> function, but in reverse:<\/p>\n<pre class=\"lang:c# theme:vs2012\">public void Restart()\r\n{\r\n        side = \"X\";\r\n        moves = 0;\r\n        gameOverPanel.SetActive(false);\r\n        SetInteractable(true);\r\n        restartButton.SetActive(false);\r\n        for (int i = 0; i &lt; spaceList.Length; i++)\r\n            spaceList[i].text = \"\";\r\n}<\/pre>\n<p>Once completed, the <em>Restart <\/em>function should look like the one in Figure 30.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"434\" height=\"218\" class=\"wp-image-79213\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-29.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 30: The <em>Restart <\/em>function.<\/p>\n<p>Now that both the <em>GameController <\/em>and <em>Space <\/em>scripts are complete, make sure you&#8217;ve saved them both and return to the Unity editor.<\/p>\n<h2>Finishing the Project<\/h2>\n<p>First, before assigning scripts to any objects, you need to make a prefab of the <em>Space <\/em>object. You&#8217;ll use the prefab to edit the <em>Space <\/em>object&#8217;s settings and then you&#8217;ll make duplicates of the <em>Space <\/em>object. The duplicates will share all the properties of the prefab you create, thus saving time. With that said, drag the <em>Space <\/em>object into your <em>Assets <\/em>window as seen in Figure 31.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"324\" height=\"787\" class=\"wp-image-79214\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-30.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 31: Creating a new prefab.<\/p>\n<p>Select the <em>Space <\/em>prefab you just created and then move to the <em>Inspector <\/em>window. Scroll down until you see the <em>Add Component <\/em>button. Click the button and search for the <em>Space <\/em>script. Once found, click on the script to add it as a component to the prefab like in Figure 32.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"573\" height=\"359\" class=\"wp-image-79215\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-31.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 32: Attaching the <em>Space <\/em>script to the <em>Space <\/em>prefab.<\/p>\n<p>You&#8217;ll need to assign the <em>Space <\/em>prefab itself as the value of the <em>Button <\/em>field as well as its child <em>Text <\/em>object to the <em>Button Text <\/em>field below like in Figure 33.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"359\" class=\"wp-image-79216\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-32.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 33: Setting the values of <em>Button <\/em>and <em>Button Text<\/em>.<\/p>\n<p>Next, in the button component, set a new <em>On Click <\/em>event by clicking the + plus button near the bottom of the component, as seen in Figure 34.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"104\" class=\"wp-image-79217\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-33.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 34: Creating a new <em>On Click <\/em>event.<\/p>\n<p>The <em>On Click <\/em>events exist for executing code whenever a button is clicked. In this case, you&#8217;ll be assign the <em>Space <\/em>script&#8217;s <strong>SetSpace<\/strong> function as the code to execute whenever a <em>Space <\/em>button is clicked on. After clicking the + button, select the object picker by clicking the small circle next to the field that should currently say <em>None (Object)<\/em> like in Figure 35.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"254\" height=\"125\" class=\"wp-image-79218\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-34.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 35: Picking a new object for the <em>On Click <\/em>event.<\/p>\n<p>In the window that appears, search for <em>Space<\/em>. Remember that you&#8217;ll want the prefab and not the script here. Search for the <em>Space <\/em>object with the blue cube next to it like in Figure 36.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"252\" height=\"168\" class=\"wp-image-79219\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-35.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 36: Selecting the <em>Space <\/em>prefab.<\/p>\n<p>After you&#8217;ve selected the object, click on the field that says <em>No Function<\/em>. A menu will appear showing the different components currently attached to <em>Space<\/em>. Go to <em>Space-&gt;SetSpace <\/em>to set the <em>SetSpace <\/em>function as the code to execute whenever a <em>Space<\/em> button is clicked. Figure 37 shows where to find everything you need for this step.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"732\" height=\"497\" class=\"wp-image-79220\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-36.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 37: Setting <em>SetSpace <\/em>as the code to execute.<\/p>\n<p>Your <em>Space <\/em>prefab is finally complete, which means you should now be able to select the <em>Space <\/em>object in the <em>Hierarchy <\/em>window and see the changes you just made applied to it. The prefab can come in handy in the future if you wanted to make additional boards or have the buttons used in different scenes. In this case, you&#8217;ve made it so that all buttons on this board will behave exactly the same way. With <em>Space <\/em>selected in the <em>Hierarchy <\/em>window, press Ctrl + D eight times to make a total of nine <em>Space <\/em>objects. Once you&#8217;ve done this, your <em>Hierarchy<\/em> will look similar to the one in Figure 38.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"304\" height=\"371\" class=\"wp-image-79221\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-37.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 38: The <em>Hierarchy <\/em>with all the <em>Space <\/em>duplicates.<\/p>\n<p>Of course, the buttons can&#8217;t all stay in one place. You&#8217;ll need to move them around the board using the <em>Rect Transform <\/em>component to have a proper Tic-Tac-Toe game board. Table 2 shows the needed coordinates for each object. It&#8217;s important to note that the right <em>Space <\/em>object is in its correct place. For example, you wouldn&#8217;t want the intended bottom right space to be in the position that upper middle space will be in.<\/p>\n<p><strong>Object Name<\/strong><\/p>\n<p><strong>Position X<\/strong><\/p>\n<p><strong>Position Y<\/strong><\/p>\n<table class=\"table--tight\">\n<tbody>\n<tr>\n<th>\n<p>Space<\/p>\n<\/th>\n<td>\n<p>-50<\/p>\n<\/td>\n<td>\n<p>100<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>Space (1)<\/p>\n<\/th>\n<td>\n<p>55<\/p>\n<\/td>\n<td>\n<p>100<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>Space (2)<\/p>\n<\/th>\n<td>\n<p>160<\/p>\n<\/td>\n<td>\n<p>100<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>Space (3)<\/p>\n<\/th>\n<td>\n<p>-50<\/p>\n<\/td>\n<td>\n<p>0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>Space (4)<\/p>\n<\/th>\n<td>\n<p>55<\/p>\n<\/td>\n<td>\n<p>0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>Space (5)<\/p>\n<\/th>\n<td>\n<p>160<\/p>\n<\/td>\n<td>\n<p>0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>Space (6)<\/p>\n<\/th>\n<td>\n<p>-50<\/p>\n<\/td>\n<td>\n<p>-100<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>Space (7)<\/p>\n<\/th>\n<td>\n<p>55<\/p>\n<\/td>\n<td>\n<p>-100<\/p>\n<\/td>\n<\/tr>\n<tr>\n<th>\n<p>Space (8)<\/p>\n<\/th>\n<td>\n<p>160<\/p>\n<\/td>\n<td>\n<p>-100<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"caption\">Table 2: All Space objects and their coordinates.<\/p>\n<p>If the new space button positions will not stick, be sure that you are running the latest version of Unity. It&#8217;s possible that your <em>GameOver Panel <\/em>object is sitting underneath the <em>Space <\/em>objects in the middle row. To fix this, you simply need to rearrange the order of the <em>GameOver Panel <\/em>in the <em>Hierarchy <\/em>like in Figure 39.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"304\" height=\"371\" class=\"wp-image-79222\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-38.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 39: Rearranging the placement of the GameOver Panel in the <em>Hierarchy<\/em>.<\/p>\n<p>Next you&#8217;ll need to select the <em>GameController <\/em>object in the <em>Hierarchy <\/em>and then attach the <em>GameController <\/em>script to it like you did with the <em>Space <\/em>prefab with the <em>Add Component<\/em> button. Once you&#8217;ve done that, you&#8217;ll need to fill in the list of text objects for the <em>Space List <\/em>array. Lock the <em>Inspector <\/em>window so it shows the <em>GameController <\/em>script by clicking the lock icon in the top right corner like in Figure 40.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"128\" height=\"87\" class=\"wp-image-79223\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-39.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 40: Locking the inspector.<\/p>\n<p>Next, expand each <em>Space <\/em>object by clicking the arrow next to each <em>Space <\/em>object. This will reveal the child <em>Text <\/em>object. Make sure they&#8217;re all selected using Ctrl + Left Mouse Click, then drag them onto the <em>Space List <\/em>field in the <em>GameController <\/em>component. Figure 41 shows how this is done.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"497\" class=\"wp-image-79224\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-40.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 41: Setting up the <em>Space List <\/em>array.<\/p>\n<p>At this point, you&#8217;ll want to make absolutely sure that the <em>Text <\/em>objects are in the right order in the <em>Space List <\/em>array. You can check this by simply clicking on an element field and seeing where the corresponding object lights up in the <em>Hierarchy.<\/em> Each object should be in order of its appearance in the <em>Hierarchy<\/em>.<\/p>\n<p>Now, assign the <em>GameOver Panel <\/em>object to the <em>Game Over Panel <\/em>field. Expand the <em>GameOver Panel <\/em>object to reveal its <em>Text <\/em>object. Drag that object into the <em>Game Over Text <\/em>field in the <em>GameController <\/em>component. Finally, click and drag the <em>RestartGame <\/em>object into the <em>Restart Button <\/em>field. When you&#8217;re done, the <em>Game Controller <\/em>script should look like the one in Figure 42.<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"294\" class=\"wp-image-79225\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-41.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 42: The finished <em>GameController <\/em>script.<\/p>\n<p>Finally, unlock the <em>Inspector <\/em>window and select the <em>RestartGame <\/em>object. Scroll down until you see the <em>On Click <\/em>event like with the <em>Space <\/em>prefab earlier. This time, click and drag the <em>GameController <\/em>object from the <em>Hierarchy <\/em>into the <em>Object <\/em>field in the <em>On <\/em>Click event. After you&#8217;ve set the object you will then click the field that says <em>No Function <\/em>and navigate to <em>GameController-&gt;Restart <\/em>to set the <em>Restart <\/em>function as the code to execute when the <em>RestartGame <\/em>button is clicked. After you&#8217;re finished, save everything and give your game a try!<\/p>\n<p class=\"caption\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"475\" class=\"wp-image-79226\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2018\/06\/word-image-42.jpeg\" \/><\/p>\n<p class=\"caption\">Figure 43: Tic-Tac-Toe in action!<\/p>\n<h2>Conclusion<\/h2>\n<p>Using only UI elements, you&#8217;ve made an entire game! The idea was to show you just a few things you can do with Unity&#8217;s built in UI systems. Typically, when creating any UI objects they are used to help inform the player of information like the current score or where they are on a map. However, UI can be used for almost any purpose you desire. The possibilities really are as endless as you can imagine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The user interface, such as buttons and messages, in a game is very important. In this article, Lance Talbert shows how to create a working Tic-Tac-Toe game using Unity and C# consisting of user interface elements. &hellip;<\/p>\n","protected":false},"author":317499,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538],"tags":[],"coauthors":[52549],"class_list":["post-79183","post","type-post","status-publish","format-standard","hentry","category-dotnet-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/79183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/317499"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=79183"}],"version-history":[{"count":16,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/79183\/revisions"}],"predecessor-version":[{"id":79244,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/79183\/revisions\/79244"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=79183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=79183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=79183"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=79183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}