This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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 (160 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 (381 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 Frequently asked questions and 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 [email protected] with your username.