Be careful of infinite loops!

In the Shake IEnumerator I wrote this:

IEnumerator Shake()
{
float elapsedTime = 0f;

    while (elapsedTime < shakeDuration)
        transform.position = initialPosition + (Vector3)Random.insideUnitCircle * shakeMagnitude;
        elapsedTime += Time.deltaTime;
        yield return new WaitForEndOfFrame();

    transform.position = initialPosition;
}

If you happen to forget curly braces ("{ }" these guys) under your while loop you won’t get any errors. Instead C# will run the first line that comes after your “while” statement. In this case it tried to transform camera position to some random place every frame. Since increment line is not executed, Unity gets stucked in an infinite loop.

Privacy & Terms