0 votes
if Input.is_action_just_pressed("ui_down"):
    var spear = SPEAR.instance()
    get_parent().add_child(spear)
    spear.position = $Position2D.global_position

The code worked until I added the position line. Can anyone tell me why this has happened?

asked Jun 24 in Engine by jhale (20 points)

Is there an error-message?
Is Position2D a child of the node with this script?

Yes it is a child of the node with this script. There is no error message when I try and shoot.

1 Answer

0 votes
Best answer

I think you don't have the Position2D as a direct child of the node that has this script. You may have to add it to your scene, or just add another path.
for example instead of the commend "$Position2D" you may have to use:

get_node("child_node1/child_node_2/Position2D")

I'm just saying, that you can't call a properity of a node, if it doesn't exists on the path, that you're giving to the function.
With this little information i could only guess the answer, but I hope it helped ;)

answered Jun 24 by Czselu349 (139 points)
selected Jun 24 by jhale
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.