Bullets aren't being destroyed when leaving the screen

I’m trying to make my bullets be destroyed when they leave the screen, but the function that should teorically do this isn’t working, does someone know why?

extends PhysicsBody2D
class_name projectile

var speed = 300
var velocity = 50

func start(_position, _direction):
rotation = _direction
position = _position
velocity = Vector2(speed, 0).rotated(rotation)

func _process(delta):
print (global.active_bullets)

func _physics_process(delta):
var collision = move_and_collide(velocity * delta)
if collision:
velocity = velocity.bounce(collision.get_normal())
if collision.get_collider().has_method(“hit”):
collision.get_collider().hit()

func _on_visible_on_screen_enabler_2d_screen_exited():
queue_free()

func _on_timer_timeout():
global.active_bullets = 0
queue_free()

Hello @Agente_B7 !

After checking your code, all seems to be good, but there are two things I am missing:

  • Code formating: Please, when you upload code, format it as code with the code formating option in the toolbar above your text or selecting it and pressing Ctrl+E. This is the only way to notice an error in your code as you might have entered a typo or a blank space that is not visible without formatting.

  • More info: We can actually see your code, but there is some missing info that should be displayed. As far as I can see, you have a signal connected to this code that should come from a VisibilityNotifier2D node. Is that signal actually connected? Where is this node in the node tree?