- Home /
Comparing game objects for my Tic Tac Toe game.
Hello. I have a tic tac toe game but instead of X's and O's the cubes change color. I am trying to figure out a way to compare a row of three cubes with each other to determine if it is a win then display the 'game over' message, but have been unsuccessful. I tried creating a bool and setting each color to T or F but it displays the game over message before the game even starts. Basically I would like to know the best way to compare three cubes with each other to see if they are the same.
Here is the color changing code, it is attached to each of the 9 cubes on the board:
public class Cubes : MonoBehaviour
{
void OnMouseOver()
{
if(Input.GetMouseButtonDown(0)){
renderer.material.color = Color.red;
Destroy(GetComponent<MeshCollider>());
}
else if (Input.GetMouseButtonDown (1)) {
renderer.material.color = Color.blue;
Destroy (GetComponent<MeshCollider> ());
}
}
}
And here is the code for my controller(I'm trying to get it to work with three objects first):
public class Controller : MonoBehaviour {
public GUIText gameOverText;
public GameObject B1;//for boxes 1-9
public GameObject B2;
public GameObject B3;
void Start () {
gameOverText.text = "";
}
void Update() {
if (??????)
{
gameOverText.text = "Game Over";}
}
}
I hope I explained my problem clearly and it makes sense. If I did not apologies. Thank you.
Answer by Olgo · Feb 17, 2014 at 05:04 AM
In addition to turning the cube red or blue, i would also associate a number (x) with each color, like 1 or 2. Then you could write some bool operations to check to see if somebody won by comparing the int.
so...
Idea 1:
//In your Cube file, add a variable that signifies which player has selected the cube
//for example, make an int called "chosenBy", set chosenBy to 0 for nobody, 1 for player 1, 2 for player 2.
Cubes cB1 = B1.GetComponent<Cubes>();
Cubes cB2 = B2.GetComponent<Cubes>();
Cubes cB3 = B3.GetComponent<Cubes>();
if( cB1.chosenBy == cB2.chosenBy && cB2.chosenBy == cB3.chosenBy ){
// top three values are equal!
}
Your Cubes file would look like this:
public class Cubes : MonoBehaviour
{
public int chosenBy;
void OnMouseOver()
{
if(Input.GetMouseButtonDown(0)){
chosenBy = 1;
renderer.material.color = Color.red;
Destroy(GetComponent<MeshCollider>());
}
else if (Input.GetMouseButtonDown (1)) {
chosenBy = 2;
renderer.material.color = Color.blue;
Destroy (GetComponent<MeshCollider> ());
}
}
}
------------------------------------------
Idea 2: changing the colors to strings and comparing the strings in the same way... You would not need to change your Cubes class for this solution
if( B1.renderer.material.color.ToString() == "red" && B1.renderer.material.color.ToString() == "red" && B1.renderer.material.color.ToString() == "red" ){
myText.text = "Red Wins!";
}
Answer by Domedi · Feb 17, 2014 at 10:46 PM
So I would populate the ToString with the colors I'm using? This is how I have it now:
public class Controller2 : MonoBehaviour {
public GUIText myText;
public GameObject B1;
public GameObject B2;
public GameObject B3;
void Start () {
myText.text = "";
}
// Update is called once per frame
void Update() {
if ( B1.renderer.material.color.ToString("red") == B2.renderer.material.color.ToString("red")) {
myText.text = "Game Over";
}
}
}
Do I make any changes in my "Cubes" file? For some reason when I do this it will display the "Game Over" text as soon as the game starts, regardless if I click or not. Interesting though, if I use the != instead of == as my comparison operator it will not display the text at all. I tried using the first solution by creating a variable in Cubes and assigning it a value based on which click the user input. Same deal as the ToString solution.
Side note; you aren't supposed to post further information as an answer, you should make it a comment to my answer. You could also edit your original question. I don't $$anonymous$$d but others will yell at you :)
$$anonymous$$eep in $$anonymous$$d these are just 2 of the simpler solutions that just popped into my head, there are endless ways you could do this.
So depending on which route you wanted to take, you would either...
A: Change your cubes file to have another variable that holds a value to compare.
or
B: Compare the strings using ToString(), adding ".ToString()" to a variable just converts whatever it is into a string of text.
I didn't really include a complete answer because I didn't know your level of understanding. I was just providing an example to point you in the right direction. I will now edit my original response to be more in depth.
Sorry about the 1 million different edits that probably popped up in your email :) You should have all the info you need now.
Got it! It didn't occur to me that all the cubes were automatically set to 0 so the game over sign flashed before the game even got going. I changed their default values in the inspector to anything but 1 or 2. That coupled with your great help got it working perfectly. Thanks a heap for your time and patience and apologies for misposting my earlier reply. Hope I got it right this time. Cheers.
Answer by himanshugupta159 · Aug 16, 2018 at 01:15 PM
link text Check the link i have written a blog related to How to make a Tick Tack Toe game. If you need any help comment on the blog i will be there.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133744im_/https://answers.unity.com/themes/thub/images/avi.jpg)