- Home /
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!
OnTriggerEnter itself can be a coroutine; you don't need to launch another coroutine from it.
Thanks for the responses. I figured out my problems, they were located in a different part of the script. Also your explanations are very useful.
Answer by DaveA · Jul 14, 2011 at 10:43 PM
You don't need 'new' in yield
Odd, that looks like it would work. It's not a static var is it?
No it is a private var. I still have no idea why it is doing this.
And removing the 'new' doesn't seem to help.
Answer by aldonaletto · Jul 16, 2011 at 12:31 AM
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.
Your answer
Follow this Question
Related Questions
Accessing variables from seperate scripts 1 Answer
Global or local variables for use at start function just once 2 Answers
Local Variables 2 Answers
Transform global quaternion from local quaternion 2 Answers
Not asigned health script 1 Answer