Error: Node not found: "Marker2D" (relative to "/root/Charc"). How to solve?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LTTS
var bullet = bullet_scene.instantiate()
bullet.position = $Marker2D.global_position
bullet.position.y = global_position.y
get_parent().add_child(bullet)

I try to make it that bullet’s starting position is on Marker2D, that’s attached to CharacterBody2D, but it simply tells that “Node not found”. Can someome please help me with that?

:bust_in_silhouette: Reply From: jgee_23_

It depends on this scripts’ position in the tree. Lets say that you have a tree:

World

–Map
----Marker2D
------Sprite2D

–Player
----Sprite2D
----Hitbox

If the script is assigned to the “Map” node, then you are able to use $Marker2D
If the script is assigned to “Marker2D”, then you can simply write global_position.
If the script is assigned to “World”, then you can write get_node(“Map/Marker2D”)
If the script is assigned to “Player”, then you can write get_parent().get_node(“Marker2D”)

Which node you assign your script to is very important, since it will make your entire project much cleaner, and much easier to navigate.

No solution seems to work, it just gives same error

LTTS | 2023-05-10 15:59