Score UI not updating

So I’m following along with the course and I seem too have hit a brick wall with the Score Ii not updating.
I have double that my scripts are correctly attached…

My hud.gd is attached to my Hud node (control node)
the script is he same as the lecture.

extends Control

@onready var score = $Score


func set_score_label(new_score):
	score.text = "SCORE: " + str(new_score)

In the game scene. the game.gd is attached to the Gane(root node). and the code for the score Ui is the same as in the lecture.

extends Node2D

var lives = 3
var score = 0

@onready var player = $Player
@onready var hud = $UI/Hud


func _ready() -> void:
	hud.set_score_label(score)

func _on_death_zone_area_entered(area: Area2D) -> void:
	area.enemy_die()



func _on_player_took_damage():
	lives -= 1
	if lives == 0:
		player.player_die()


func _on_enemy_spawner_enemy_spawned(new_enemy) -> void:
	new_enemy.connect("enemy_died", _on_enemy_died)
	add_child(new_enemy)


func _on_enemy_died():
	score += 100
	hud.set_score_label(score)

But my score does not update when the enemy dies

Did I miss something?
I am not sure if this is relevant , but im using Godot 1.2.1

Nothing jumps out at me - I thought this was the same issue I looked at yesterday, but this is happening with the regular enemy instead of the path enemy.

Can you post your spawner and enemy scripts as well?

enemy_spawner script:

extends Node2D

const ENEMY = preload("res://scenes/enemy/enemy.tscn")
@onready var spawnpositions: Node2D = $Spawnpositions

signal enemy_spawned(new_enemy)

func _on_timer_timeout() -> void:
	spawn_enemy()


func spawn_enemy() -> void:
	var spawn_positions_array = spawnpositions.get_children()
	var random_spawn_position = spawn_positions_array.pick_random()
	
	
	var new_enemy = ENEMY.instantiate()
	new_enemy.global_position = random_spawn_position.global_position
	emit_signal("enemy_spawned", new_enemy)
	#add_child(new_enemy)

Enemy script:

extends Area2D

signal enemy_died

@export var enemy_speed = 300
# Called when the node enters the scene tree for the first time.

func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass


func _physics_process(delta: float) -> void:
	enemy_movement(delta)



func enemy_movement(delta) -> void:
	global_position.x -= enemy_speed * delta


func enemy_die() -> void:
	queue_free()
	emit_signal("died")


func _on_body_entered(body: Node2D) -> void:
	body.take_damage()
	enemy_die()

I have not gotten to adding the path enemy as yet, this is just regular enemy being randomly spawned in… They get removed when they come into contact with the player or the rockets… I just cannot figure out why the score is not updating when an enemy dies from a rocket

Pretty sure I got it. In your enemy script, you are emitting a signal called "died", but your signal is called "enemy_died" - I’m betting you just renamed it at some point =)

You are correct! I had changed it so I would not get confused between the enemy and player die signals. I forgot to change it in the emit_signal in the enemy script, and It was staring me right in the face all this time, but I just was not seeing it!.

The score is now being updated!
Ty for the second pair of eyes, and the assist!.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms