I want the bullets i fired to dissapear after 5 seconds from spawning

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

Here is a code i thinked about:

if (Bullet is spawned/exists for 5 seconds):
   queue_free()
:bust_in_silhouette: Reply From: Scavex

Sure ! You can create a timer for that either by code or by adding Timer node in your Bullet scene. I think you should add a Timer node in your Bullet Scene. How ? Select your Bullet Scene and add a Timer node to it just like you add Sprite, CollisionBody2D etc nodes. After that select that Timer Node and go to Inspector Tab on the rightmost part of engine where you can set the Wait Time to 5 and One Shot to true. After this just go to Node Tab that is on the right side of Inspector tab and under signals connect the timeout to your bullet script. It will create a function in your bullet script where you simply have to add queue_free() statement

You can also make a Timer using code like this :

func _ready():
    bullet_free_timer = Timer.new()
	bullet_free_timer.set_wait_time(5) # 5 seconds wait time
    bullet_free_timer.set_one_shot(true)
	add_child(bullet_free_timer)
	shoot_timer.start()
	shoot_timer.connect("timeout", self, "remove_bullet")

func remove_bullet():
        queue_free()
:bust_in_silhouette: Reply From: clemens.tolboom

Add a Timer to your bullet

func _ready():
    $Timer.one_shot = true
    $Timer.autostart = true
    $Timer.wait_time = 2.0

func _on_Timer_timeout():
    queue_free()
:bust_in_silhouette: Reply From: DangerousScore

So i found out how to do it, just add timer to object and then this piece of code.

func _ready():
$Timer.connect("timeout", self, "queue_free")
$Timer.set_wait_time(2)
$Timer.start()

The rest of answers didn’t work for idk what reason