The player node doesn’t exist at the NodePathplayer i.e. the
node structure isn’t like this:
|>Node2D (that the bottom script you pasted is attached to) |->player
Your node structure is correct (looks like described above) but your player node is deleted at some point via queue_free() or free()
If it’s #1, you just need to fix your NodePath on the line that I listed so it correctly points to the player node RELATIVE to the node that script is attached to. CAPITALIZATION IS IMPORTANT WITH NodePaths!!!player is NOT the same as Player
If it’s #2 this code fix, while not being ideal, should remove the error:
# other code:
extends Node2D
func input(event: InputEvent) -> void:
if event.isactionpressed("shoot"):
var bullet := preload("res://bullet.tscn").instance()
var player_node = get_node_or_null("player")
if player_node:
bullet.rotation = player_node.degforbullet
bullet.globalposition = $player/Position2D/shotgun/muzzle.globalposition
addchild(bullet)
Well I fixed the error but now its not shooting anything.
code I changed:
extends Node2D
func input(event: InputEvent) → void:
if event.is_action_pressed(“shoot”):
var bullet := preload(“res://bullet.tscn”).instance()
var player_node = get_node_or_null(“player”)
if player_node:
bullet.rotation = player_node.degforbullet
bullet.globalposition = $player/shotgun/muzzle.globalposition
add_child(bullet)
sup0088 | 2021-11-10 12:46
If you changed your code to that and it doesn’t put the bullet next to your player node then your NodePath is wrong (#1). Post a screen shot of your SceneTree (left side of your editor where all the nodes are) and I’ll be able to tell you what your NodePath should be.