Can't kill the player

Godot Version

Godot 4.2.2

Question

Hello. I am trying to make a game in 2D where the player dies when it touches a spesific object. The problem is that i can get it working on one level, but if i try it on another level there is a error that pops up.

Code

extends AnimatableBody2D

@onready var player = $"../player"

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	rotation += 25

func _on_saw_death_part_body_entered(body):
	if (body.name == "player"):
		player.DIE()

This is the error that pops up:

E 0:00:16:0695   saw_script.gd:3 @ _ready(): Node not found: "../player" (relative to "/root/Node2D/enemies/AnimatableBody2D5").
  <C++ Error>    Method/function failed. Returning: nullptr
  <C++ Source>   scene/main/node.cpp:1651 @ get_node()
  <Stack Trace>  saw_script.gd:3 @ _ready()

Probably because your tree ordering doesn’t match with the path you’re using. But why you’re using this @onready var player = $"../player"? Your _on_saw_death_part_body_entered(body) already give you the node that touches the area, you could just call body.DIE()

1 Like

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