Destroying a area3d with a raycast

Godot Version

4.1.1

Question

So I’m trying to make a basic interaction system prototype with a raycast 3d and a area3d. I’m just trying to detect the area with the raycast, then once its detected, allow the player to press the interact key and destroy the whole object. The hope was to just figure out if I can detect the object and execute the code, but when I do, the detection code freaks out because the thing its detecting is now a null instance, but its within an if statement that relies on the raycast colliding. I’m a little confused because shouldn’t the code within the if statement not run if there’s nothing to collide with? Why is it giving me errors that the object is a null instance when it shouldn’t even be running at all if that’s the case?

Here is the code , this is running within physics process on my player script. I’ve tried a couple different iterations with resources first but now I’m just using a simple object script that only has a is_interactable bool and the method explode() with queue.free() inside.

func _physics_process(delta):

if interactcast.is_colliding():
	var obj = interactcast.get_collider()
	if obj.is_in_group("object") and obj.is_interactable:
		pickup_label.text = "Pick up " + obj.name
		if Input.is_action_just_pressed("interact"):
			obj.explode()
else:
	pickup_label.text = ""

The label works fine, but once interact is pressed and the object deletes itself the error message is “Attempt to call function “is_in_group” in base “null instance” on a “null instance”” but if interactcast.is_colliding() returns false then shouldn’t that be irrelevant? Any insight on what I’m missing here would be greatly appreciated! Thank you for reading!

I have figured it out with the help of a friend! If anyone else is stumped on this like I was the solution is a simple check before any of the logic in the if statements goes off

if interactcast.is_colliding():
	var obj = interactcast.get_collider()
	if obj != null:
		if obj.is_interactable:
			pickup_label.text = "Pick up " + obj.name
			if Input.is_action_just_pressed("interact"):
				obj.explode()
		else:
			pickup_label.text = obj.name
	
else:
	pickup_label.text = ""

Hopefully this helps out any newcomers to gdscript like myself. If your code is upset that something disappears make sure you add a check to see if its null before doing anything!

!= compares a value, but null is a type, so operator is should be used:

if obj is not null:

</ pedantic>

(someone correct me if I’m wrong but in Python this is called “equality vs. identity”

When I replace the operator as you suggested I get an error actually. “Expected type specifier after is” so I guess “not” is not the correct syntax here? != seems to work for my purposes right now so I’m not really concerned about it haha

if not (obj is null):

or maybe also

if not obj is null:

or simply:

if obj:

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