Ruby Gosu crash error: undefined method `*' for nil:NilClass

36 Views Asked by At
def update
        if button_down?(Gosu::KB_SPACE) && !@pressed
            @y -= 30
            @pressed = true
        elsif !button_down?(Gosu::KB_SPACE)
            @pressed = false
        end
        if @vel.nil?
            @vel *= 4
            @y += @vel * 0.05
        end
    end

everytime i add an operator for @vel it gives me this error. Why's that?

tried checking if the variable is nil to run but none worked for me

1

There are 1 best solutions below

0
On

The conditional @vel.nil? only returns true when @vel is nil. You want the opposite, either use [email protected]? or use the unless keyword instead of the if keyword before @vel.nil?.