Hello.
Read most of questions but didn’t find an answer yet.
I’m making a Tic-Tac-Toe game.
For One player I just use yield waitforseconds, and random O pops up in the empty cell.
But I’d’ love to make it “Hot seat” style. So here is the question:
How do I switch from X to O when using my mouse to click?
And if taking TURNS is the trick, how do I start a turn? I mean, where do I hang the script for the turn to be started.
Thank you.
1 Answer
1Run this example script, understand the logic, then incorporate it into your game.
Example script
public enum Player { ONE, TWO };
public class Turn : MonoBehaviour {
public Player currentPlayer = Player.ONE;
void OnGUI() {
string display = ( currentPlayer == Player.ONE )? "Player One" : "Player Two";
if ( GUILayout.Button( display + ": Click to change Player" ) ) {
NextPlayer();
}
}
void NextPlayer() {
if( currentPlayer == Player.ONE ) {
currentPlayer == Player.TWO;
}
else if( currentPlayer == Player.TWO) {
currentPlayer == Player.ONE;
}
}
}
You can then use the currentPlayer to control either X/O is put in the grid when user click on a grid. Then after a X/O is put on the grid, you check if anyone have won the game or the grid is fully filled, if not, NextPlayer().
for very simple timers in Unity, use invoke(), don't bother with yield and coroutines.
– Fattie