- Home /
TBG,If,While
Im trying to make a turn based game for a project,it will be a remake of the risk game.I have made this code(it is the phase when the players choose their territory) and i m trying to find an elegant way to arrange the phases of the game.My main problem is in the update function.With this if statement it constanty prints the end turn untill the unity crashes,i have tried also with a while loop and some coroutines,but when i run the script whith the whilw loop the unity stops responding,albeit i make the condition false and about the coroutines,i dont understand them very well,but is seems that they are not useful in this point.It will be thankfull if anyone of you could help me and sorry if my english are poor,i think i made my point.
var referenceCamera: Transform;
public var displayText : GUIText;
var hit : RaycastHit;
var playerOneChoose = new Array(); var playerTwoChoose = new Array();
private var Choose = true;
private var playerOne = true;
public var playerOneArmy = new Array();
public var playerTwoArmy = new Array();
function Update (){
if (Choose == true
&&playerTwoChoose.length + playerOneChoose.length <= 25){
ChooseOne();
ChooseTwo();
}
else{
PhaseChooseEnd();
}
}
function ChooseOne(){
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (playerOne == true){ displayText.text = "Player One";
if (Input.GetMouseButtonDown(0)) {
if(Physics.Raycast(ray, hit)
&& hit.collider.renderer.material.color != Color.red){
hit.collider.renderer.material.color = Color.green;
playerOneChoose.Add(hit.collider);
print ("P1");
playerOne=!playerOne;
}
}
}
}
function ChooseTwo(){
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (playerOne == false){ displayText.text = "Player Two"; if (Input.GetMouseButtonDown(0)) {
if(Physics.Raycast(ray, hit)
&& hit.collider.renderer.material.color != Color.green){
hit.collider.renderer.material.color = Color.red;
playerTwoChoose.Add(hit.collider);
print ("P2");
playerOne = true;
}
}
}
}
function PhaseChooseEnd(){
if (playerTwoChoose.length + playerOneChoose.length == 26){ Choose = false; print ("end turn");
}
}
Answer by Arcadewiz · Jul 30, 2014 at 09:24 AM
Use Debug.log("Choose value : " + Choose) at the places in your code where you are trying to check for change in the boolean value.
Place different Debug.Log in your Update if else commands to see when the conditions are being met.
I have a feeling your if in update statement might not be working properly.