I am currently having issues with this code from the internet.
onready var reach = $Player/Pivot2/Camera/Reach
onready var hand = $Player/Hand
onready var axemodel = preload('res://Scenes/Axe.tscn')
func _process(_delta):
if reach.is_colliding():
if reach.get_collider().get_name() == "Axe":
weapon_to_spawn = axemodel.instance()
else:
weapon_to_spawn = null
else:
weapon_to_spawn = null
if hand.get_child(0) != null:
if hand.get_child(0).get_name() == "Axe":
weapon_to_drop = axemodel.instance()
else:
weapon_to_drop = null
if Input.is_action_just_pressed("TakeItem"):
if weapon_to_spawn != null:
if hand.get_child(null) != null:
get_parent().add_child(weapon_to_drop)
weapon_to_drop.global_transform = hand.global_transform
weapon_to_drop.dropped = true
hand.get_child(0).queue_free()
reach.get_collider().queue_free()
hand.add_child(weapon_to_spawn)
weapon_to_spawn.rotation = hand.rotation
What this script is trying to do is search for nearby items that can be grabbed, and if you press E on them you will equip it. If you have any item in your hand you will drop it and equip a new one.
However this line right here throws an error:
if hand.get_child(0) != null:
error:
E 0:00:05.648 get_child: Index p_index = 0 is out of bounds (data.children.size() = 0).
<C++ Source> scene/main/node.cpp:1296 @ get_child()
<Stack Trace> World.gd:33 @ _process()
I can't get my head around this and i don't know how to fix it as it seems an engine error but what do i know. Sorry if this is a dumb question i'm new to Godot