Queue freeing raycast collider causes error: Attempt to call function 'get_name' in base 'null instance' on a null instance

Godot Version



In my game, I have a raycast node which detects areas and lets the player interact with them. The issue is that when queue freeing a collider, in my case, the parent node of the area that is being detected, the raycast returns the error in the title, as if it were checking for that specific area in every frame.

Here’s part of the raycast 3D’s code:

func _process(_delta):
	if self.is_colliding() == true:
		if self.enabled == true and Input.is_action_just_pressed("interact"):		
			change_player_pos.emit(get_collider().get_name()) #Get name returns error when queue free is done on collider

Maybe what is happening is you raycast detecting an collision in _process call and start the another if check, in that time _physics_process do their circle and the collision can stop happening, so when you call get_collider will return null because the collision didn’t happen anymore, you should check directly what get_collider has and if is not null:

func _process(_delta):
	# Get whatever is colliding or null if no collision happens
	var collision_result = get_collider()

	# Check if collision_result is null
	if collision_result:
		# This self.enabled check doesn't make sense, you'll just 
		# have collision if the raycast is enabled, otherwise no
		# collision will happen
		#if self.enabled == true and Input.is_action_just_pressed("interact"):
		if Input.is_action_just_pressed("interact"):		

Thanks, it seems that checking if get_collider() != null was enough to make it work

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.