Coroutine (WaitForSeconds()) in "OnCollisionEnter()" gives compile error

Solution 1:

You can only yield in a coroutine function. You can't from a void function. Some Unity callback functions like the Start function can be made a void or coroutine function. Luckily, the OnCollisionEnter function is one of them so simply change the void to IEnumerator. This will work without a need to manually start a new coroutine function. Unity will automatically call and start it as a coroutine when there is a collision.

private IEnumerator OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.name == "Platform(Clone)")
    {
        numberOfJumps = 2;
        Debug.Log("Platform hit");
    }

    //Maake platforms fall
    yield return new WaitForSeconds(2f);
    collision.rigidbody.useGravity = enabled;
    yield return null;
}

Solution 2:

You should make a Coroutine and then call it inside of OnCollisionEnter. Void means that there is no return type for the method, which is why you got the compile errors.

IENumerator Fall(Rigidbody rigidbody)
{
    //Make platforms fall
    yield return new WaitForSeconds(2f);
    rigidbody.useGravity = enabled;

}

private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.name == "Platform(Clone)")
    {
        numberOfJumps = 2;
        Debug.Log("Platform hit");
    }
    StartCoroutine(Fall(collision.rigidbody));
}