I am creating a simple Tic Tac Toe game in Unity 3D using only UI elements, as part of my course, still a learner. Now i wanted to add the function when i click on a button and its text property changes to X, it also changes color to red, and when it’s O to change its color to green. Any ideas how i can write that script?
Here is my GameControl script if useful
public GameObject Menu;
public GameObject Game;
public Dropdown DropdownP1;
public Dropdown DropdownP2;
public Text Instruction;
public Text GameState;
[HideInInspector]
public string CurrentSymbol;
public List<Text> Field = new List<Text>();
public bool endGame = false;
public AudioSource audio;
public Image win;
// Start is called before the first frame update
void Start()
{
win.enabled = false;
Menu.SetActive(true);
Game.SetActive(false);
CurrentSymbol = "X";
}
// Update is called once per frame
void Update()
{
if (CurrentSymbol == "X")
{
if (DropdownP1.value == 0)
{
GameState.text = "PLAYER 1";
}
if (DropdownP2.value == 0)
{
GameState.text = "PLAYER 2";
}
}
else if(CurrentSymbol == "O")
{
if (DropdownP1.value == 1)
{
GameState.text = "PLAYER 1";
}
if (DropdownP2.value == 1)
{
GameState.text = "PLAYER 2";
}
}
}
public void EndTurn()
{
//condition to end game
//horizontal
if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[1].text && CurrentSymbol == Field[2].text)
{
endGame = true;
}
if (CurrentSymbol == Field[3].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[5].text)
{
endGame = true;
}
if (CurrentSymbol == Field[6].text && CurrentSymbol == Field[7].text && CurrentSymbol == Field[8].text)
{
endGame = true;
}
//vertical
if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[3].text && CurrentSymbol == Field[6].text)
{
endGame = true;
}
if (CurrentSymbol == Field[1].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[7].text)
{
endGame = true;
}
if (CurrentSymbol == Field[2].text && CurrentSymbol == Field[5].text && CurrentSymbol == Field[8].text)
{
endGame = true;
}
//diagonal
if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[8].text)
{
endGame = true;
}
if (CurrentSymbol == Field[2].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[6].text)
{
endGame = true;
}
if (endGame == true)
{
if (CurrentSymbol == "X")
{
if (DropdownP1.value == 0)
{
GameState.text = "PLAYER 1 WINS";
}
if (DropdownP2.value == 0)
{
GameState.text = "PLAYER 2 WINS";
}
}
else
{
if (DropdownP1.value == 1)
{
GameState.text = "PLAYER 1 WINS";
}
if (DropdownP2.value == 1)
{
GameState.text = "PLAYER 2 WINS";
}
}
sound.Play();
win.enabled = true;
CurrentSymbol = "END";
}
if (CurrentSymbol == "X")
CurrentSymbol = "O";
else if (CurrentSymbol == "O")
CurrentSymbol = "X";
}
public void OnStart()
{
Menu.SetActive(false);
Game.SetActive(true);
CurrentSymbol = "X";
endGame = false;
for (int i = 0; i < Field.Count; i++)
{
Field*.text = "-";*
Field*.GetComponentInParent().interactable = true;*
}
}
public void OnEndGame()
{
Menu.SetActive(true);
Game.SetActive(false);
win.enabled = false;
}
public void OnDropdownP1()
{
if (DropdownP1.value == 0)
{
Instruction.text = “Player 1 will play first”;
DropdownP2.value = 1;
}
else if (DropdownP1.value == 1)
{
DropdownP2.value = 0;
}
}
public void OnDropdownP2()
{
if (DropdownP2.value == 0)
{
Instruction.text = “Player 2 will play first”;
DropdownP1.value = 1;
}
else if (DropdownP2.value == 1)
{
DropdownP1.value = 0;
}
}
}
,I am creating a simple Tic Tac Toe game in Unity 3D using only UI elements, as part of my course, still a learner. Now i wanted to add the function when i click on a button and its text property changes to X, it also changes color to red, and when it’s O to change its color to green. Any ideas how i can write that script?
Here is my GameControl script if useful:
public GameObject Menu;
public GameObject Game;
public Dropdown DropdownP1;
public Dropdown DropdownP2;
public Text Instruction;
public Text GameState;
[HideInInspector]
public string CurrentSymbol;
public List Field = new List();
public bool endGame = false;
public AudioSource audio;
public Image win;
// Start is called before the first frame update
void Start()
{
win.enabled = false;
Menu.SetActive(true);
Game.SetActive(false);
CurrentSymbol = “X”;
}
// Update is called once per frame
void Update()
{
if (CurrentSymbol == “X”)
{
if (DropdownP1.value == 0)
{
GameState.text = “PLAYER 1”;
}
if (DropdownP2.value == 0)
{
GameState.text = “PLAYER 2”;
}
}
else if(CurrentSymbol == “O”)
{
if (DropdownP1.value == 1)
{
GameState.text = “PLAYER 1”;
}
if (DropdownP2.value == 1)
{
GameState.text = “PLAYER 2”;
}
}
}
public void EndTurn()
{
//condition to end game
//horizontal
if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[1].text && CurrentSymbol == Field[2].text)
{
endGame = true;
}
if (CurrentSymbol == Field[3].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[5].text)
{
endGame = true;
}
if (CurrentSymbol == Field[6].text && CurrentSymbol == Field[7].text && CurrentSymbol == Field[8].text)
{
endGame = true;
}
//vertical
if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[3].text && CurrentSymbol == Field[6].text)
{
endGame = true;
}
if (CurrentSymbol == Field[1].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[7].text)
{
endGame = true;
}
if (CurrentSymbol == Field[2].text && CurrentSymbol == Field[5].text && CurrentSymbol == Field[8].text)
{
endGame = true;
}
//diagonal
if (CurrentSymbol == Field[0].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[8].text)
{
endGame = true;
}
if (CurrentSymbol == Field[2].text && CurrentSymbol == Field[4].text && CurrentSymbol == Field[6].text)
{
endGame = true;
}
if (endGame == true)
{
if (CurrentSymbol == “X”)
{
if (DropdownP1.value == 0)
{
GameState.text = “PLAYER 1 WINS”;
}
if (DropdownP2.value == 0)
{
GameState.text = “PLAYER 2 WINS”;
}
}
else
{
if (DropdownP1.value == 1)
{
GameState.text = “PLAYER 1 WINS”;
}
if (DropdownP2.value == 1)
{
GameState.text = “PLAYER 2 WINS”;
}
}
sound.Play();
win.enabled = true;
CurrentSymbol = “END”;
}
if (CurrentSymbol == “X”)
CurrentSymbol = “O”;
else if (CurrentSymbol == “O”)
CurrentSymbol = “X”;
}
public void OnStart()
{
Menu.SetActive(false);
Game.SetActive(true);
CurrentSymbol = “X”;
endGame = false;
for (int i = 0; i < Field.Count; i++)
{
Field*.text = “-”;*
Field*.GetComponentInParent().interactable = true;*
}
}
public void OnEndGame()
{
Menu.SetActive(true);
Game.SetActive(false);
win.enabled = false;
}
public void OnDropdownP1()
{
if (DropdownP1.value == 0)
{
Instruction.text = “Player 1 will play first”;
DropdownP2.value = 1;
}
else if (DropdownP1.value == 1)
{
DropdownP2.value = 0;
}
}
public void OnDropdownP2()
{
if (DropdownP2.value == 0)
{
Instruction.text = “Player 2 will play first”;
DropdownP1.value = 1;
}
else if (DropdownP2.value == 1)
{
DropdownP1.value = 0;
}
}
}