Why is my function not being called?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Brisben88

I have a simple shoot’em up in space. When enemies collide with missiles, they explode and disappear. I also send a signal (enemyHit) to HUD to update and display the score. I have declared the signal, connected it to a function in HUD, yet, the function does not get called (the 2 print methods in the function are for debugging purposes). Thanks for any help.

The missile code:

extends RigidBody2D

signal enemyHit

export (PackedScene) var Explosion

func _on_VisibilityNotifier2D_screen_exited():
    queue_free()

func _on_Missiles_body_entered(body):
        
    if !body.is_in_group("player"):
        emit_signal("enemyHit")
        var explosion_instance = Explosion.instance()
        explosion_instance.position = get_global_position()
        get_tree().get_root().add_child(explosion_instance)
        queue_free()
        $CollisionShape2D.set_deferred("disabled", true)

The HUD code:

extends CanvasLayer

var score = 0

func _ready():
    score = 0

func _on_Missiles_enemyHit():
    score += 1
    print (str(score))
    print ("function called")
    $ScoreLabel.text = str(score) 
:bust_in_silhouette: Reply From: Inces

I don’t see signal enemyhit being connected anywhere, and it should be somewhere in those two scripts :). And I don’t think You did it in editor, because your separate missile scene propably hasn’t HUD in its tree.

Another thing - You say You want to emit signal when enemy is hit, but You check if body hit by missile is in group “player” ?. Shouldn’t it be “enemy” ?.