yield WaitForSeconds causing simple booleon script problem

Hi , I have a simple script that is intended for spikes that go up and down continuously and my script works fine for that by looking at the starting position then seeing if its over or below it however I’m trying to add a small delay using the yield WaitForSeconds(); statement but it’s causing a problem. I have two variables Moveup and Movedown that change depending on the y.position of the object which then call a function but when using yield WaitForSeconds();one of the variables Moveup doesn’t switch to false so both variables are stuck on true making the object unable to move. I was just wondering what I was doing wrong as the script works fine without the yield WaitForSeconds(); statement.

thanks :slight_smile:

heres the code

var startPos: float;
var Currentypos: float;
var Moveup = true;
var Movedown= false;


function Start()
{
  startPos = transform.position.y;
  
}

function Update()
{

if(Moveup){
 this.transform.position += transform.up *0.1;
 }

if(Movedown){
 this.transform.position += transform.up *-0.1;
}

Currentypos = transform.position.y;

if(Currentypos > startPos+5){
	Spikedown();
	
	}

if(Currentypos < startPos-1){
	Spikeup();
}

	}

function Spikedown(){
	Moveup = false;
	Movedown = true;
	
	
	
	}

function Spikeup(){
Movedown= false;
[B]yield WaitForSeconds(1);[/B]
Moveup = true;
}

id put an else if on the move ups and move downs, and on the currentpos, to make sure only one is happening at a time. try that

Spikeup is a coroutine and you need to call it like this:
StartCoroutine(Spikeup());

or, if you want to be able to specifically stop the coroutine by name of function do:
StartCoroutine(“Spikeup”);

Thanks for the help , the else trick worked and now I feel stupid :slight_smile: