help with an incomprehensible error in GODOT 4

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

hi
I would like to ask for help
I can’t say what the problem
is, but I’ll try to describe it
in general:
I am implementing a crafting system
and I have a tree and a stone
and I can hit a tree with a stone and when the tree has fallen
, it gives a stone
, but when I add part of the crafting system code
that we check variables and
after the tree falls, the tree script gives me an error
(Invalid set index ‘position’ (on base: ‘null instance’) with value of type ‘Vector2’.)
and after deleting the code responsible for the crafting script
, everything starts working as it should (except crafting)
here is the tree code:

[ TREE SCRIPT ]
extends Area2D

@export var hp = 20

func _ready():
$anim.play(“ilde”)

func _process(delta):
if hp <= 0:
$anim.play(“dead”)

func destroy():
var drop = preload(“res://prefab/item.res”).instantiate()
get_parent().add_child(drop)
drop.position = global_position

func take_damage(damage):
hp -= damage
$anim.play(“take”)

func _on_area_entered(area):
if area.is_in_group(“tool_low”):
take_damage(1)
elif area.is_in_group(“tool_med”):
take_damage(2)
elif area.is_in_group(“tool_full”):
take_damage(3)

[ ITEM SCRIPT ]

extends Area2D

@export var key = ‘’
@export_multiline var decs = ‘’

var enter = false
var pickup = false
var craft = false
var craft_id = 0
var count = 0

@export var _damage = 1

@export var kd = 1
var kd_end = 0

@onready var player = get_parent().get_node(“player/inventory”) # link to the player
@onready var world = get_parent().get_node(“.”) # link to the map

func _ready():
$name.hide()

func _process(delta):
kd_end -= 0.005
if kd_end <= 0:
$anim.play(“ilde”)
kd_end = 0

if Input.is_action_just_pressed(“use”) and enter == true:
if player.get_child_count() < 1:
add()
if Input.is_action_just_pressed(“drop”) and pickup == true:
drop()

if pickup == true:
use()

if craft == true and enter == true:
if Input.is_action_just_pressed(“craft”):
if craft_id == 1:
var spawn = preload(“res://prefab/tree.res”).instantiate()
get_parent().add_child(spawn)
spawn.position = player.global_position
queue_free()

func use():
look_at(get_global_mouse_position())
$art.offset.x = 2
$art.flip_v = get_global_mouse_position().x < global_position.x
if Input.is_action_pressed(“LMB”) and kd_end == 0:
if get_global_mouse_position().x < global_position.x:
$anim.play(“use_left”)
else:
$anim.play(“use_right”)
kd_end = kd

func add():
pickup = true
global_position = player.position
get_parent().remove_child(self)
player.add_child(self)
rotation_degrees = randi_range(-180, 180)
$body.disabled = true
$craft/coll.disabled = true

func drop():
pickup = false
get_parent().remove_child(self)
world.add_child(self)
global_position = player.global_position
rotation_degrees = 0
$body.disabled = false
$art.offset.x = 0
$craft/coll.disabled = false

func _on_body_entered(body):
if “player” == body.name:
enter = true
$name.show()

func _on_body_exited(body):
if “player” == body.name:
enter = false
$name.hide()

func _on_craft_area_entered(area):
if area.is_in_group(“item”):
craft = true
craft_id = 1

func _on_craft_area_exited(area):
if area.is_in_group(“item”):
craft = false
craft_id = craft_id

there is a video of the problem at the bottom:
https://youtu.be/waEGYXbJn_s

please help me, thank you in advance

:bust_in_silhouette: Reply From: Gluon

I think your issue is this line

drop.position = global_position

what global position? I suspect you probably intended this

drop.position = self.global_position

Code Don’t work

BOX_Milk | 2023-03-09 15:17