Im making a chest and it is supposed to drop a key. When i open it it closes the current play test and im not very good with GD script so i dont know how to fix it

extends AnimatedSprite

export(PackedScene) var object_scene: PackedScene = null

var is_player_inside: bool = false
var is_opened: bool = false

onready var animation_player: AnimationPlayer = get_node("AnimationPlayer")
onready var tween: Tween = get_node("Tween")

func _ready() -> void:
assert(object_scene != null)
animation_player.play("Idle")

func _input(event: InputEvent) -> void:
if event.is_action_pressed("Interact") and is_player_inside and not is_opened:
is_opened = true
animation_player.play("Open")

func _drop_object() -> void:
var object: Node2D = object_scene.instance()
owner.get_node("Objects").add_child(object)
func on_Area2D_player_entered(player: KinematicBody2D) -> void:
is_player_inside = true

func on_Area2D_player_exited(player: KinematicBody2D) -> void:
is_player_inside = false

It says the problem is right here:

func _drop_object() -> void:
var object: Node2D = object_scene.instance()
owner.get_node("Objects").add_child(object)

function 'add_child' in base 'null instance' on a null instance
at function: _drop_object

if anyone knows whats wrong please help

  • xyz replied to this.

    LoseriamVM By the looks of it, the node referred to by the variable owner, doesn't have a child named "Objects".

    I think I made this same mistake because I used instance() instead of instantiate().

    instance is correct here since this is Godot 3.

    Note that owner is null for the root node of a packaged scene. If this script is attached to the root node then it can not work (the owner. part would be unnecessary in this case).