Invalid operands 'nil' and 'int' in operator '+'

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ShingleBells

I keep getting the above error when i try to call this function:

func gain_exp(amount):
    cur_exp += int(amount)
    total_exp += int(amount)

If it helps, I’m declaring and assigning the variables like this:

var total_exp
var cur_exp

func _ready():
    cur_exp = 0
    pass
:bust_in_silhouette: Reply From: volzhs

you only declare variable, not set value.
so, var total_exp is same as var total_exp = null

you can simply fix it by assigning default value.

var total_exp = 0
var cur_exp = 0