4.1.2
Question
so i tried to add a scene into another scene for the game im making but everytime i try to run the game the error is: “attempt to call funcion add child in base null instance on a null instance”. Can anyone help? Here is the code:
func xp_bar():
var xpbar = $"../CanvasLayer/xpbar"
xpbar.value = global.player_xp_gain
if xpbar.value >= xpbar.max_value:
global.counter += 1
global.player_xp_gain = 0
print("level ", global.counter)
xpbar.max_value = 100 * global.counter
var options = 0
var options_max = 3
while options < options_max:
var option_choice=item_option.instantiate()
upgrade_options.add_child(option_choice)
options += 1
also forgot to say that the function is put into ready func…
I’m not very good at coding, but I think the problem might be what you’re adding the child to and what youre adding.
heres what i did for an example
|
var food1 = preload(res://node_2d.tscn) |
|
var food2 = food1.instantiate() |
|
var food3 = $Camera2D |
|
add_child(food2) |
|
food3.add_child(food2) |
you dont really say what “upgrade_options” is nor do you say what “option_choice” is.
the debug is basically telling you that both of those things are null.
so make sure the item_option is something like
||var food1 = preload(res://node_2d.tscn)||
and then make sure upgrade_options is something like
var food3 = $Camera2D
it should be green
you could also just do add child(without the upgrade_options) and it will add the child to the node you have the script on
for example:
add_child(option_choice)
and then it will add the child without needing what to add it to.