Attempt to call function 'is_in_group' in base 'null instance' on a null instance.

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

I would like to have the turret to shoot out bullet when raycast hits the Player’s group.

Below is my code:
func shoot()->void:
if can_shoot and ray.is_colliding():
**if ray.get_collider().is_in_group(“Player”) :
can_shoot = false
var temp = Bullet.instance()
temp.global_position = global_position
temp.rotation_degrees = pointer.rotation_degrees + 90
temp.setup(ray.get_collision_point() - position)

The bold part keeps causing the issue as :
Attempt to call function ‘is_in_group’ in base ‘null instance’ on a null instance.

I’m trying to understand and figure out a solution for this. Can anyone help?

:bust_in_silhouette: Reply From: JCJL

The function get_collider() returns null if there are no colliders present so you’re trying to get the group of a null instance, so you my wanna check if its even colliding. You can either just use masks and make it so that the RayCast2D detects ONLY the player so you can just use:

if ray.is_colliding():

Or in your case not knowing which masks are you using or if youre using any, then you could just check if the collider is null first like so:

if ray.get_collider()!=null:
   if ray.get_collider().is_in_group("player"):
:bust_in_silhouette: Reply From: aidave


var collider = ray.get_collider()

And see what you get stepping through the debugger. This seems really fishy that is_colliding works but then getting the collider doesn’t. Is it possible you’ve freed the collider in the same frame before this call?

I’m guessing something else got killed in front of the ray, so the best solution would be what JCJL said, to use masks instead of groups, or you can check is_instance_valid first, but that is hacky.