Unity being selective about which commands to execute.

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.

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.

This never happens. If lines aren't executing, it's 100% guaranteed that you made a logic error.

As far as I can tell, unity seems to run the scripts "sideways", meaning that each line in the script is run simultaneously.

No, that's physically impossible; a CPU can't do that. (With multiple cores, yes, but that's a different issue and doesn't apply here; everything in Unity code is one thread unless you explicitly use System.Threading.)

I haven't really looked at your code closely; it's very difficult to tell what's happening with formatting like that. Clean it up and you may see where the problem is. Also, it looks like you're scheduling events, in which case it would be simpler to use coroutines instead of a ton of if/then stuff in Update.

Alright, for the benefit of any unfortunate souls out there who have this same issue, I have found two solutions.

First, which is highly undesirable because of the way I have the game set up, I can end the script with this line.

buildPoint = null;

The other solution is to increase the range of detection by changing this

            minTargX = (buildPoint.transform.position.x - 1);
            minTargZ = (buildPoint.transform.position.z - 1);
            maxTargX = (buildPoint.transform.position.x + 1);
            maxTargZ = (buildPoint.transform.position.z + 1);

to this

    minTargX = (buildPoint.transform.position.x - 3);
    minTargZ = (buildPoint.transform.position.z - 3);
    maxTargX = (buildPoint.transform.position.x + 3);
    maxTargZ = (buildPoint.transform.position.z + 3);

which makes no sense to me, but ok, wtf ever.