I have a game with several units on the field, some computer controlled, some player controlled. when the unit's AP reaches 0, a boolean called 'isitmyturn' is turned off. and the script should make the next unit's 'isitmyturned' boolean enabled in the queue.
However, as soon as the first unit's turn ends, the index value immediately sets itself out of the arrays index range. I'm not sure where it is going wrong, here is the script,
var order : GameObject[];
var selectedunit : GameObject;
var index : int = 1;
var numberofunits : int = 2;
function nextturn(){
if(selectedunit.tag == "Enemy"){
selectedunit.GetComponent(enemyAI).isitmyturn = true;
}
if(selectedunit.tag == "Player"){
selectedunit.GetComponent(player_script).isitmyturn = true;
}
index += 1;
}
function Update(){
var selectedunit : GameObject = order[index];
if(index > (numberofunits-1)){
index = 0;
}
if(selectedunit.tag == "Enemy"){
if(selectedunit.GetComponent(enemyAI).isitmyturn == false){
nextturn();
}}
if(selectedunit.tag == "Player"){
if(selectedunit.GetComponent(player_script).isitmyturn == false){
nextturn();
}}
}
↧