Coroutines and if statements

I have this code here:

void speed()
	{
		//Debug.Log (LeftRightMovement.speed);
		if(GUICounters.speed == 1)
		{
			//Debug.Log (LeftRightMovement.speed);
			LeftRightMovement.speed = 60;

			StartCoroutine(timer ());
			//Debug.Log (LeftRightMovement.speed);
		}
	}

	IEnumerator timer()
	{
		Debug.Log (LeftRightMovement.speed);
		yield return new WaitForSeconds(5);
		LeftRightMovement.speed = 30;
	}

My players speed is currently 30, when LeftRightMovement.score reaches a certain point I would like to increase the speed to 60 for 5 seconds using the coroutine and then set the speed back to 30 but as it is the speed is constantly at 60.

I also tried wrapping it in a boolean as i think its because it runs every frame and my score is always at 1. This is what i tried:

void speed()
	{
		if(ranOnce = false)
		{
			//Debug.Log (LeftRightMovement.speed);
			if(GUICounters.speed == 1)
			{
				//Debug.Log (LeftRightMovement.speed);
				LeftRightMovement.speed = 60;

				StartCoroutine(timer ());
				//Debug.Log (LeftRightMovement.speed);
			}
			ranOnce = true;
		}

	}

But i had no luck with that either, nothing comes out form the Debug.Log() then.

If anyone could point me in the right direction, that’d be great

Your boolean method would work if your condition would look like this:

    if(ranOnce == false)

Instead of

    if(ranOnce = false)
  • = assignment operator
  • == equals operator