Home > Enterprise >  Local variable 'verticalMovementSpeed' might not be initialized before accessing
Local variable 'verticalMovementSpeed' might not be initialized before accessing

Time:04-26

So i made this method in a unity c# script but its giving the error 'Local variable 'verticalMovementSpeed' might not be initialized before accessing'

Code:

float VerticalAirMovementSpeed(bool IsGrounded, float verticalMovementInput)
{
    //Makes forward and backward sensitivity (vertical sensitivity) controllable separately form eachother
    float verticalMovementSpeed;
    //forward and backward movement speed in air and on the ground
    if (verticalMovementInput < 0 && !IsGrounded)
    {
        verticalMovementSpeed = backwardAirMovementSpeed;
    }
    else if (verticalMovementInput > 0 && !IsGrounded)
    {
        verticalMovementSpeed = forwardAirMovementSpeed;
    }
    else if (verticalMovementInput > 0 && IsGrounded)
    {
        verticalMovementSpeed = forwardMovementSpeed;
    }
    else if (verticalMovementInput < 0 && IsGrounded)
    {
        verticalMovementSpeed = backwardMovementSpeed;
    }
    
    return verticalMovementSpeed * verticalMovementInput;
}

(C#)

CodePudding user response:

You could just start with:

float verticalMovementSpeed = float.Parse("0.00");

That way it would be initialized.

  • Related