-2

I am trying to call a coroutine that sets some variables, waits a few seconds, and then sets them back.

Unfortunately, the entire coroutine is firing all at once, without waiting. In this case, it's almost as if the function is never called at all.

public void Dash(){
    lastDashTime = Time.time + dashWaitDuration;

    motionBlur (); //this call is not working as expected

    animator.SetFloat ("Speed", 0);
    playerRigidBody.MovePosition (playerRigidBody.position + transform.forward * 1.75f);
}


//this is the coroutine
IEnumerator motionBlur(){
    print ("Hello");
    camMotionBlur.jitter = 10;
    camMotionBlur.enabled = true;

    yield return new WaitForSeconds (2);

    camMotionBlur.enabled = false;
    camMotionBlur.jitter = 0.125f;
}
rutter
  • 11,242
  • 1
  • 30
  • 46
r0128
  • 461
  • 1
  • 4
  • 10
  • It's returning an enumerator object that's waiting for you to enumerate it before it executes everything from `yield return` to the end of the `motionBlur` method. You never do that. You discard the `IEnumerator` unused. Is this a common Unity idiom? I don't know from Unity at all. – 15ee8f99-57ff-4f92-890c-b56153 Nov 28 '16 at 20:20

2 Answers2

6

If you are trying to start a coroutine in C#, then you will need to call StartCoroutine:

StartCoroutine(motionBlur());
rutter
  • 11,242
  • 1
  • 30
  • 46
Sam Marion
  • 690
  • 1
  • 4
  • 17
2

You need to call IEnumerator with StartCoroutine. Check here. Here is the code:

StartCoroutine(motionBlur ());

After that your code will work.

OsmanSenol
  • 146
  • 5