Cannot call method 'instantiate' on a null value

I’m trying to have one scene spawn another by using a packed scene and keep getting this error.

@export var firebomb_scene:PackedScene

func _on_body_entered(body):
	if player.subweapon != player.NOSUB:
		if player.subweapon == player.AXE:
			pass
		elif player.subweapon == player.FIREBOMB:
			var firebomb = firebomb_scene.instantiate()
			var bomb_position
			bomb_position = 16
			firebomb.global_position = global_position
			print("Player direction is:" + str(player.player_direction))
			if player.player_direction == player.LEFT: 
				bomb_position *= -1
			firebomb.global_position.x += bomb_position 
			get_tree().current_scene.add_child(firebomb)
	game_manager.add_axe()
	print("Testing axe pickup.")
	animation_player.play("pickup")

Instead of using export can you try to use preload(“scene-path”) and see if it works?

1 Like

i’ll see if i can figure out how to do that, thanks

# you can just drag the packedscene from your file-window in the parenthesis
var firebomb_scene: PackedScene = preload("")
1 Like

That seems to work in terms of getting rid of the error and making the scene appear where it’s supposed to. Unfortunately the player is unable to pickup the item that is spawned, so I’m gonna have to figure that out. Thanks so much!

1 Like

Yes the problem with export is, that it sometimes gets defined very late in the script for some reason

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.