- Home /
Yield statement is not working
I'm trying to make a speed pickup that only lasts for 3 seconds, but right now the speed boost is lasting forever. Here is my code:
public enum PickupType { Health = 0, Speed = 1 }; public PickupType pickupType = PickupType.Health;
bool ApplyPickup (PlayerScript playerStatus) { print("ApplyPickup() called"); switch (pickupType) { case PickupType.Health: healthPickup(); break;
case PickupType.Speed:
print("Speed pickup found");
PlayerScript.moveSpeed += 50000;
StartCoroutine("speedPickup");
break;
}
return true;
}
public IEnumerator speedPickup ()
{
print("speedPickup() running");
yield return new WaitForSeconds(3);
print("3 seconds passed");
PlayerScript.moveSpeed = PlayerScript.originalSpeed;
print("moveSpeed reverted to original");
}
void OnTriggerEnter (Collider col) { if (!ApplyPickup (playerStatus)) return; }
The code runs just fine, with no errors, but the last two messages, "3 seconds passed" and "moveSpeed reverted to original" are the only ones that don't appear. What's wrong with my code? :/ Does it have anything to do with being in a Switch statement?
OnTriggerEnter. I edited my original post to include where ApplyPickup() is applied.
What is the variable pickupType? Where is its value set?
Is pickupType being changed to PickupType.Speed somewhere, and you're sure "speedPickup() running" is shown?
Answer by lampshade · Oct 26, 2010 at 05:16 AM
Try to implement something like this into your code:
private bool isRunning = false;
public IEnumerator MyEnumFunc() { isRunning = true; // Running
for (int i = 0; i < 30 i++) {
isRunnning = false; // Stopped or something
yield return new WaitForSeconds(0.1f); // Wait for 3 seconds!
}
isRunning = true; // Running again
}
Your answer
