Editing local variables in another script

I am working on a waypoint script and am having trouble with global variables.

It works by triggers. When the waypoints detect a collision from an AI, they advance a counter on the AI in order for it to go to its next waypoint. What is does now is add to the counter on a Global scale, not local. This causes all of the AI to move even if only one is triggered. How can I alter the variable on only the local level?

The following code is on each individual waypoint.

`
function OnTriggerEnter(other : Collider)
{
   MoveNext(other);
}
function MoveNext(other : Collider)
{
	yield new WaitForSeconds (10);
	other.GetComponent(AI).IncreaseWayPoint();
 }
`

This next code is on the AI

`
private var currentWaypoint : int;
function IncreaseWayPoint ()
	{
		currentWaypoint++;

	}
`

Also, a little off topic, why doesn’t the yield command work here?

Thanks!

You don’t need ‘new’ in yield

Odd, that looks like it would work. It’s not a static var is it?

There’s a problem with that yield: it will start a 10s pause, then change the waypoint - but if the enemy enters the trigger again before the pause ends, another independent cycle will be started, and 10s after the waypoint will be incremented again. If the enemy enters and exits the trigger several times in a few seconds, several cycles will be started, with the enemy changing waypoints 10s after each OnTriggerEnter event.

Maybe I’m wrong, but I suppose your idea is to create a 10s dead time during which no more waypoint increments are allowed. If this is true, you should change the logic. The trigger enter should be:

function OnTriggerEnter(other : Collider)
{
	other.GetComponent(AI).IncreaseWayPoint();
}

And the AI script should do the following:

var busy: boolean = false;

function IncreaseWayPoint (){
	if (!busy){ // does nothing if there's an increment cycle going on
		busy = true; // signal a cycle has started
		yield WaitForSeconds(10); // starts pause
		currentWaypoint++; // 10s after inc waypoint
		busy = false; // cycle ended
	}
}

I hope this does what you want to do; if not, please let me know.

NOTE: If you really want a dead time, it would be better to increment the waypoint before the pause.