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.
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
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.