This is a very annoying problem that I have had far more times than I should. I guess it's because I'm an old school programmer, and I'm used to command lines being executed from first to last, but it seems that unity likes to pick and choose which command lines it feels like executing. As far as I can tell, unity seems to run the scripts "sideways", meaning that each line in the script is run simultaneously. In this particular case, it simply refuses to toggle the booleans "amDone" and "move". I've tried moving them around in the script and even moving the toggle operations into their own function, all with the same result. They will not toggle. Here is the whole script.
var rotSpeed : float;
var walkSpeed : float;
var buildPoint : Transform;
private var minTargX : float;
private var minTargZ : float;
private var maxTargX : float;
private var maxTargZ : float;
var move : boolean;
private var myX : float;
private var myY : float;
private var myZ : float;
var house : Transform;
var amDone : boolean = false;
var townCenter : Transform;
//-------------------------------------
function Update(){
myX = transform.position.x;
myY = transform.position.y;
myZ = transform.position.z;
if (buildPoint) {
minTargX = (buildPoint.transform.position.x - 1);
minTargZ = (buildPoint.transform.position.z - 1);
maxTargX = (buildPoint.transform.position.x + 1);
maxTargZ = (buildPoint.transform.position.z + 1);
if (myX > minTargX && myX <= maxTargX && myZ > minTargZ && myZ <= maxTargZ){
move = false;}
else{
move = true;}
if (move == true){
var rotation = Quaternion.LookRotation(buildPoint.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotSpeed);
transform.Translate(0, 0, Time.deltaTime * walkSpeed);
}
else {
if (amDone == false){
BuildAHouse();
TownCenter.housesBuilt ++;
}
}
}}
//------------------------------------------------
function BuildAHouse(){
transform.LookAt(townCenter);
houseIBuilt = Instantiate(house, Vector3(myX,2.9,myZ), transform.rotation);
transform.parent = houseIBuilt;
transform.localPosition = Vector3(0, -2, 6);
transform.parent = null;
DidIt();
}
function DidIt(){
amDone = true;
move = false;
}
So, by all logic, the builder should stop seeking the build point after building the house and teleporting to the outside, yet amDone and move will not toggle, causing the builder to simply run straight into the wall of the house, instead of waiting for the next command. Any insight on this will be much appreciated, as this is a very frustrating issue. These are simple commands, and there is absolutely no reason why these two lines of code should not execute. Sorry for the rant.