Hi. I am making a Tic Tac Toe game, The issue I am having is I am wanting two players. 0 and 1. I have a "master script" that has the variable Player. I am wanting my boxes to know who's turn it is by asking the master script. The trouble is I cannot get them to interact with each other? What am I missing? I have 9 boxes arranged in 3 lines of 3. My scripts are: Code (csharp): static var Player = 0; Code (csharp): var Cross : Material; var Naught : Material; var Blank : Material; var object : GameObject; var clicked = 0; var TTTBoard : GameObject; var Turn = TTTBoard.GetComponent(Board).Player; function OnMouseEnter () { if (Turn == 1) { if (clicked == 0) { object.renderer.material = Cross; GetComponent(MeshRenderer).enabled = true; } if (clicked == 1) { } } if (Turn == 0) { if (clicked == 0) { object.renderer.material = Naught; GetComponent(MeshRenderer).enabled = true; } if (clicked == 1) { } } } function OnMouseExit () { if (clicked == 0) { object.renderer.material = Blank; GetComponent(MeshRenderer).enabled = false; } if (clicked == 1) { } } function OnMouseDown () { if (Turn == 1) { object.renderer.material = Cross; GetComponent(MeshRenderer).enabled = true; clicked = 1; object.GetComponent( "ChangeBoxMaterial" ).enabled = false; Turn = 0; } if (Turn == 0) { object.renderer.material = Naught; GetComponent(MeshRenderer).enabled = true; clicked = 1; object.GetComponent( "ChangeBoxMaterial" ).enabled = false; Turn = 1; } }
try making it public so other scripts are allowed to access it: Code (csharp): public static var Player = 0; you can then simply access this variable in other scripts with: Code (csharp): scriptname.variable = value; // for you it might be: masterscript.Player = 1;
The material now changes depending on the player but when I click player = 1 instantly goes back to player = 0? Ok. I have realized that when I call the Code (csharp): object.GetComponent( "ChangeBoxMaterial" ).enabled = false; it cancels the variable change. Is there a code I can use to make it stick when the code that changes it has been disabled?
About Turn Create an enum with players and start a coroutine that will wait for action. On Update() function you can manage who has turn. A small example(maby is totally worng, i never tried to make something like this) TTTManager - http://pastebin.com/wFynSsZv TTTBox - http://pastebin.com/Y1Hdj2yf you can know who's turn by checking Code (csharp): if (TTTManager.TTTTurn == TTTManager.Turn.???) { /* do */ }