OnTriggerEnter problems

I wrote my own waypoint script that currently only has 2 waypoints attached to it, i will add more once i iron out this annoying bug.

when my code hits the OnTriggerEnter function I have the console output what waypoint it is currently on. Each time my object hits the trigger it will execute the code 2 or 3 times.
this causes it to skip over multiple waypoints if I have them.

also once it hits the last waypoint and the waypointIndex is equal to the waypointarray.Length it fails to reset back to zero

is there a way to have the OnTriggerEnter function to only be called once every 2 seconds or so?
it seems that the engine executes the code so far that the OnTriggerEnter function gets called several times before it acquires a new target…

here is my console log.

The Current Waypoint is:0
UnityEngine.MonoBehaviour:print(Object)
Waypoint:OnTriggerEnter(Collider) (at Assets/Waypoint.js:36)

******* at this point the object hits the trigger and i get this in the Console instantly

The Current Waypoint is:1
UnityEngine.MonoBehaviour:print(Object)
Waypoint:OnTriggerEnter(Collider) (at Assets/Waypoint.js:36)

The Current Waypoint is:2
UnityEngine.MonoBehaviour:print(Object)
Waypoint:OnTriggerEnter(Collider) (at Assets/Waypoint.js:36)

The Current Waypoint is:3
UnityEngine.MonoBehaviour:print(Object)
Waypoint:OnTriggerEnter(Collider) (at Assets/Waypoint.js:36)

var target : Transform;
var waypoints : Transform[];
var moveSpeed : double;
var waypointIndex : int;




function Awake()
{
	if(!target)
	{
		target = waypoints[0];
	}
	waypointIndex = 0;
	print("The Current Waypoint is:" + waypointIndex);
	
}

function Update () 
{
	transform.LookAt(target);	
	transform.Translate (0,0,Time.deltaTime * moveSpeed);
}

function OnTriggerEnter (gameObject : Collider)
	{
			if(waypointIndex == waypoints.Length)
		{
			waypointIndex = 0;
 		   	print("The Current Waypoint is:" + waypointIndex);
 		}
 		

			target = waypoints[waypointIndex];
			waypointIndex++;
			print("The Current Waypoint is:" + waypointIndex);
	}

any advice or information about this would be very helpful

thanks

print out the name of the collider it’s triggering to make sure it’s only hitting a single collider, is your character hitting other colliders that aren’t waypoints?

i have been fooling around with it for quite a while and i cant seem to find out int he scripting reference exactly how i would print out what object my character has collided with.

any quick tips on this?

Make sure you use the right variable, looks like your using gameObject as you’re collider variable so you need to use gameObject.name instead of collider.name.

function OnTriggerEnter (gameObject : Collider)
{
              print(gameObject.name);
}