Function in Update delaying involuntarily

I have a section of code that I wanted to move out of update to prepare some organization for the future. Inside of update the segment runs as expected, but once I place the code segment into a separate function and call it I won’t get a response until several seconds after it was suppose to trigger.

I literally copy and pasted between the two and there is only an ‘if’ statement in the function so far.

function Update () {
    var forward : Vector3 = transform.TransformDirection(Vector3.forward);
    var back : Vector3 = transform.TransformDirection(Vector3.back);
    Debug.DrawRay (transform.position, forward, Color.white);
    
    if (Physics.Raycast(transform.position, forward, hit, 1))
        if (hit.collider.gameObject.name ==  "Player")
        {
            var test = hit.collider.gameObject.GetComponent(ThirdPersonController);
            if (test.travelAxisCurrent == 4)
                if (!Physics.Raycast(transform.position, back, hit, 1))
                {
                    AssignDestination();
                }
        }
        else{}//was planning on using this else, haven't yet.
        
    var speed = direction * Time.deltaTime;
    transform.position.z += speed;
    
    //AtDestination();
    if (transform.position.z + speed < destinationZ)
    {	
        transform.position.z = destinationZ;
        direction = 0;
    }
}

function AtDestination()
{
    if (transform.position.z + speed < destinationZ)
    {	
        transform.position.z = destinationZ;
        direction = 0;
    }
}

EDIT: as requsted, all relevant code. As is with the conditional in Update it runs perfectly, but if I comment out the conditional and un-comment the call the reaction is delayed acouple of seconds. I did try to figure this out but I’m at a complete loss as to why putting an if statement in a method would cause such behavior.
And sorry for the readability of the function, I can’t get the code formatting to cooperate.

EDIT 2: Finally found the issue. It skipped my mind that speed isn’t global. I really should stop working with global variables. So now my assumption is that once the block was pushed it just kept going until the block passed some arbitrary amount that was where the new un-initialized speed variable was. Thanks, and I guess sorry I bugged you all with something so noobish.

What is can directly see is that you defined a speed variable inside of update. That means this speed variable is only available in Update. In your AtDestination function you also use a speed variable, but it’s definately not the same as the one used in Update.

I guess you have a speed variable in your class / script itself and want to use this instead of creating a local variable in Update. So just remove the “var” in front of speed and you’re good to go.

speed = direction * Time.deltaTime;

edit
Next thing that is a bit strange is you check only if the position along the world axis is below your destinationZ. Do you only move backwards or what’s the purpose of the check?

you will have to use coroutine in update (). for example click here.