boosting speed temporarily

Hello,

I am trying to have a speed boost effect for my character where their speed is boosted for y seconds. I have a script that adjusts their speed but when I try to use the WaitForSeconds method I am having trouble. Here is my code. The variable walkSpeed is a global variable within the class.

function CWS(){
		var newSpeed = 50.0;
		var timeDuration = 10;
		var defaultWalkSpeed = walkSpeed;
		walkSpeed = newSpeed;
		yield WaitForSeconds(timeDuration);
		walkSpeed = defaultWalkSpeed;
	}

Thanks for any help. I have determined that the problem is with the yield line. Whenever I call the method with the yield line there the method never triggers. However without the yield line it runs just fine.

If you’re calling a function with Invoke, then it won’t run if the function uses yield.

–Eric

I am calling the function from another script by using

var platformScript : PlatformController = GameObject.FindWithTag("Player").GetComponent("PlatformController");
		platformScript.movementSystem.CWS();

You should try calling the script using the StartCoroutine function.

I am using javascript and according to

I shouldn’t need to call StartCoroutine. But that seemed to get it working. Thank yall so much for the help. Interesting that it says I don’t need to call it when I really did.