Waypoint / Yield help

Hi guys

Basically Im trying to make a object go from waypoint[0] to waypoint[1] and when it reaches both its meant to pause for 5 seconds using yield. But instead it carries on in a loop. Ive tried everything I can think of, no idea what to do.

I based my code off of this guys Waypoint/Animation help - Questions & Answers - Unity Discussions if that helps.

var waypoint : Transform[];
var speed : float = 4;
private var currentWaypoint : int;
var test;


function Start() 
{			
	StartCoroutine("CoStart");
}

function CoStart() : IEnumerator
{
	while (true)
		yield CoUpdate();
}

function CoUpdate () : IEnumerator
{
	
	if(currentWaypoint < waypoint.length)
        {
            var WPtarget : Vector3 = waypoint[currentWaypoint].position;
            var moveDirection : Vector3 = WPtarget - transform.position;
            var velocity = rigidbody.velocity;
 			
 			
 			
            if(moveDirection.magnitude < 1)
            {                        	  	
            	test = true; 
            	speed = 0;
            	if(test)
            	{
            		WaitFiveSeconds();
            		currentWaypoint++;
            		
            	}	  
            }             
            else
            {
               	if(!test)
               	{
               		speed = 4; 
              		
               	}
            }
            if (currentWaypoint > 1)
            {            	
               	currentWaypoint = 0;
            } 
        velocity = moveDirection.normalized*speed; 
    	rigidbody.velocity = velocity;    	
		}
}
    	
function WaitFiveSeconds() 
{
	
	WaitForSeconds(5);	 	
	test = false;	
}

Like I said, Ive tried everything I can think off, maybe you guys can help :slight_smile:

if I’m not mistaken you are using “waitForSeconds” totally incorrectly. it’s more like

yield WaitForSeconds(5)

urge you to just use Invoke() for simple timers for beginners in Unity, rarely necessary to use yield.

urge you to SEARCH on here for literally 1000s of questions on using yield correctly. your code is not right at all. you will find literally 1000s of questions on using yield correctly

also unityGEMS.com