I am brand new. I’ve only made a couple of tutorial games in the past and learned scripting from a few lessons on GDscript. My goal with this script is to have a bullet shoot at the player by an enemy (spawned randomly from a pool of 5 different markers).
But no matter what I do, my player node is returning null. And because of that, when the bullet is meant to shoot at the player, my game crashes. I have looked in the remote tab to make sure the node path is correct, I have tried both “get_node()” and CharacterBody2d = Player as well as making node a unique path by adding %.
Appreciate the well thought out question and providing us all of that information. Two things:
The problem is with your scene tree. We need to see a screenshot of that to see what your problem is.
When you post code, please copy and paste it instead of using screenshots. It’s hard to read and it creates a lot of work if we want to give you alternate code because we have to type it out ourselves. When you paste it put ``` on it’s own line above and below the code and it will be formatted correctly.
I’m not sure unique names are meant to be used across different scenes like that. Also, when you use get_node() you are only looking for local nodes in the same scene. You might want to try get_tree().current_scene.get_node() instead… But that’s not a good approach.
If you want a global reference for your player you might want to use groups or an autoload that saves the player reference.
For the groups approach, make a group named player and put only the player inside that, then you get the reference by calling get_tree().get_first_node_in_group(“player”).
For the autoload approach, let’s say you create a script named game.gd:
extends Node
var player
Then you set it as an autoload in the ProjectSettings and on the player _init() or _ready() function you call:
func _init():
Game.player = self
And when you need a player ref you can get it by calling Game.player from anywhere: