Raycast3D's detecting a null object?

Godot Version

4.7

Question

I have this code that is used to detect which side of a ladder so the player could jump away off it.

func ladder_raycast_detection_for_all_states(): ### (IN PROCESS())
	if raycast_body.is_colliding():
		### GET WHICH SIDE OF LADDER TO APPLY JUMP OFF
		var raycast_body_collider = raycast_body.get_collider()
		var raycast_body_collider_normal = raycast_body.get_collision_normal()
		if raycast_body_collider.is_in_group("ladder"):
			ladder_normal = raycast_body_collider_normal
			
		state = States.LADDER

But occasionally when it collides with object NOT in a group “ladder” it throw this error:

E 0:00:45:163 ladder_raycast_detection_for_all_states: Attempt to call function ‘is_in_group’ in base ‘null instance’ on a null instance.

I’m not sure why it occasionally detects a null instance. Shouldn’t this “if raycast_body.is_colliding() + if raycast_body_collider.is_in_group(“ladder”)” only detect an object in a group “ladder”?

get_collider() is returning null for some reason, the grouping shouldn’t matter your error is specifically about calling a function on null not that something is or isn’t part of a group.

Very odd since the is_colliding() check passes therefor the get_collider() function should return something valid. Do you use await in this function, that could cause errors. Did you edit this sample down before posting it? You could ignore the error by adding another check, but this may only make it harder to find errors with the ladder.

if raycast_body_collider != null and raycast_body_collider.is_in_group("ladder"):

No I don’t have await in my script and this is the full code.


func ladder_raycast_detection_for_all_states(): ### (IN PROCESS())
	if raycast_body.is_colliding():
		### GET WHICH SIDE OF LADDER TO APPLY JUMP OFF
		var raycast_body_collider = raycast_body.get_collider()
		var raycast_body_collider_normal = raycast_body.get_collision_normal()
		if raycast_body_collider.is_in_group("ladder"):
			ladder_normal = raycast_body_collider_normal
			
		state = States.LADDER
		
		if is_on_floor():
			state = States.FLOOR
			if Input.is_action_pressed("up"):
				velocity.y += 1
		elif !is_on_floor():
			state = States.LADDER

I can’t reproduce the error consistently so it’s hard to know what is wrong.

I would try

if is_instance_valid(raycast_body_collider) and raycast_body_collider.is_in_group("ladder"):

Very odd since the is_colliding() check passes therefor the get_collider() function should return something valid

I’m still curious to find out what could possibly be the cause. I see this error once a full moon for awhile and I just kinda brushed it off until now.

My raycast size is 0.5m and in the latest error instance my player simply walked, with normal walking speed, into a collider cube and the error occurred when raycast was still inside the cube. Any idea?

Is there any way you are deleting the “collider cube” from another action? Like the player “picks up” a coin by walking into it?

Oh yes, I actually have 2 different cubes in the same position and the culprit is probably the one that has call_deferred(“queue_free”) on it. Thank you.

(Although I recall error also happens when I hit some other wall or platform too but i’ll mark this solved for now. If it happens again somewhere else I will make another thread.)