RayCast2D detecting collisions when it shouldn't

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SingingApple
:warning: Old Version Published before Godot 3 was released.

Ok, so I am new to Godot. Been playing around it for, let’s say a few weeks.

So I started to create a game, and I got a problem that I couldn’t solve. So here is a over-over-over-simplified version of this problem.

I have created two KinematicBody2D nodes. And then placed them one on top of the other. Each has a sprite, collision_shape2D and a ray. I put them both in a node2D node. I attached a script to the KinematicBody2D node on top which keeps printing the object colliding below it.

Somewhat like this:

if ray.is_colliding() :
	print (str(ray.get_collider().get_name()))

ray here is the RayCast2D attached to the node to check and print the colliding object below it.

I also attached a script to the KinematicBody2D node below it such that it gets destoyed (aka queue_free() ) whenever it gets clicked with the mouse.

So we expect the KinematicBody2D node on top to keep printing the name of the KinematicBody2D node on bottom (as that is the node that is getting collided with the node on top) But the problem arises when we try to click on the node on the bottom to destroy it. We get an error.

So apparently I understood why the problem arose, it is because the node on top got the information that it is colliding with an object below it (first line of code), but after that the node tries to print its name which leads to the error. Apparently, the node on the bottom gets destroyed before the node on top has the chance to execute the second line of code. So when it does execute the second line of code, no one is colliding with it so we get an error when we try to ray.get_collider() .

So, how do I fix this error? HELP!

Never mind, I think I solved the issue… kinda. But what did work was simply adding a
if ray.get_collider() != null: statement between the two shown in the question to make it something like this :

if ray.is_colliding() :
    if ray.get_collider() != null:
        print (str(ray.get_collider().get_name()))

I don’t know why this works, if you know please leave it as an answer. But it works and that is all that matters.

SingingApple | 2017-12-29 15:28

Do you think there is some code in the script of the bottom KinematicBody2D to interfere with the collider code? Because when the code for the top KinematicBody2D requires that collider, it’s not showing up on click.

Ertain | 2017-12-29 20:01

What do you mean by code that could interfere with the collider of the top KinematicBody2D? Know of any things that could have happened. I am quite new so…

Anyway, thanks for trying to help me out. It works fine right now.

SingingApple | 2017-12-30 10:08

That’s good that you solved the problem.

Ertain | 2017-12-30 22:38

:bust_in_silhouette: Reply From: SingingApple

Never mind, problem solved. (Read the comments to this question if you want to know how)