0 votes

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.

Godot version 3.3.3.stable
in Engine by (119 points)

1 Answer

+1 vote
Best answer

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;

by (360 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.