Waypoint system: loop and back-forth operations

Well, i am still improving my waypoint system, now i made a script to hold all waypoints as a Vector3 array, and a script in the object that i want to follow the path. All is working fine when i make my object loop trough the waypoints, like this:

0 -> 1 -> 2
0 <------ 2

But now i want to the object to follow the waypoint path backwards, then forward again (patrol style), like this:

0 -> 1 -> 2
0 <- 1 <- 2

Here’s my waypoint script, wich is attached to a gameobject. This script is used to obtain the waypoints and display some graphical stuff in the editor:

    Waypoint.js

@script ExecuteInEditMode()
#if (UNITY_EDITOR)
var waypoints : Vector3[];
@HideInInspector
var length : int;
function OnDrawGizmos () {
	for (a = 0; a <= waypoints.length-1; a++) {
		if (a == waypoints.length-1) {
			Gizmos.color = Color.red;
			Gizmos.DrawLine(transform.position + waypoints[a],transform.position + waypoints[0]);
		}else{
			Gizmos.color = Color.yellow;
			Gizmos.DrawLine(transform.position + waypoints[a],transform.position + waypoints[a+1]);
		}
		Gizmos.color = Color.blue;
		Gizmos.DrawSphere(transform.position + waypoints[a], 0.2);
	}
}
#endif
function GetWaypoint(w : int) : Vector3{
	return transform.position + waypoints[w];
}
function Update() {
	length = waypoints.Length;
}

And this is my navigation script, wich handles the position of the object wich it is attached:

    AnimatePosition.js
var waypoints : Waypoint;
var speed : float;
var loop : boolean = false;
private var direction : boolean = false;
private var time : float;
private var state : int = 0;
function Start () {
	time = Time.time;
}
function Update () {
	if (state == waypoints.length) {
		if (loop == true) {
			state = 0;
		}else{
			direction = true;
		}
	}
	if (state == 0 && direction == true) {
		direction = false;
	}
	Debug.Log(direction);
	transform.position = Vector3.Lerp(transform.position, waypoints.GetWaypoint(state), (Time.time - time)*speed);
	if (Mathf.Round(transform.position.x * 100) / 100 == Mathf.Round(waypoints.GetWaypoint(state).x * 100) / 100 && Mathf.Round(transform.position.y * 100) / 100 == Mathf.Round(waypoints.GetWaypoint(state).y * 100) / 100 && Mathf.Round(transform.position.z * 100) / 100 == Mathf.Round(waypoints.GetWaypoint(state).z * 100) / 100) {
		if (direction == false) {
			state++;
		}else if (direction == true){
			state = state-1;
		}
		time = Time.time;
		Debug.Log(state);
	}
}

This code works fine with the “Loop” option selected (private var loop : boolean), but when i deselect it (to the back-and-forth operation) it starts erroring out about the array index being out of range. Probably, like my other questions, it will be a stupidly trivial error i can’t notice.

Nevermind, i found the solution after further experimentation. I was forgetting to subtract the `state` when the direction was going `true`. Changed bit of code: (Right under the Update() declaration in AnimatePosition.js)

if (state == waypoints.length) {
    if (loop == true) {
        state = 0;
    }else{
        direction = true;
        state--;
    }
}