Struggling with Referencing and adding score

Godot Version

4

Question

I’m a beginner and making a game similar to space invaders. i want to make it so that when the player dies, score gets added. i’ve defined the score variable in the Game Manager node and the death logic in the enemy itself, however, i get an error where the Game Manager returns as nill

I understand that the enemy is somehow not getting reference to the Game Manager and is returning nothing

I have not added the Enemy to the main sceen and that is why its returning the Game Manager nil. I tried adding the enemy before the game started and the enemy that was already in the main scene ended up working just fine, but any other enemy spawned afterwards just crashed the game and gave me the error. Here is my Enemy Script and Node Structure:

How do i fix this?

Unique node names are very limited, they are supposed to be used within the same scene (in your case Enemy would have to have a GameManager child) to help shorten long path names, not to actually access a unique node among the entire game.

Sounds like you would rather be using a Global/Autoload

Thanks! worked like a charm!

I’m gonna be a bit broad,

I think what you want is when the “ennemy” dies, your score goes up.

You have 2 choices, I imagine you spawn your ennemy in some way from GameManager.

  1. You can to have your GameManager connect a signal from your ennemy to itself when it spawns it, and then you add you score.

or

  1. You make a script named “score” and add it as a AutoLoad, which means it can be references from any nodes.

  2. You do not want to check every frame if the health of the ennemy is lower than 0, you want to add a function called take_damage, and if the ennemy hp is lower, then you call add_score.