Godot Engine just ignores "if instance != null"

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

Here is the code below, it worked in version 3.2.3 but it stopped working for some reason. I think this may be due to the fact that I switched to the new version 3.3.3. Any way to fix this?

func bullet():
	var bullet_instance = BULLET.instance()
	bullet_instance.apply_impulse(Vector2(), Vector2(bullet_speed, 0).rotate	d(rotation))
	get_tree().get_root().add_child(bullet_instance)
	yield(get_tree().create_timer(bulletDestroyTime), "timeout")
	if bullet_instance != null:
		bullet_instance.queue_free()

Error message: Attempt to call function 'queue_free' in base 'null instance' on a null instance.

:bust_in_silhouette: Reply From: The_Black_Chess_King

Try is_instance_valid(instance:Object) instead of bullet_instance != null:

From docs on GDScript

bool is_instance_valid(instance: Object)

Returns whether instance is a valid object (e.g. has not been deleted
from memory).

Maybe you don’t need to check for queue_free()? In my understanding if bullet_instance != null: will always return true because you defined the instance above, so you don’t need that if;