How am I able to get the "get_collider()" code to work

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

So, I keep getting this message saying "Attempt to call function ‘get_collider’ in base ‘null instance’ on a null instance. I have no clue how to fix it. I followed a tutorial saying that I needed to enter in this code, I have no idea why it didn’t work because it worked for the person making the tutorial, but here is the code that’s giving me a problem:

		if $Raycast2D2.get_collider("/root/Proto/Player"):
		var player = get_parent().get_parent().get_node("/root/Proto/Player")

Well first off get_collider() doesn’t take a parameter so that code is just wrong.

It is failing because at the time you are calling the get_collider() function (if it even works with being called with "/root/Proto/Player" as the parameter) the engine can’t find a child node of the node that this script is attached to named Raycast2D2 so $Raycast2D2 returns null and null is just a fancy way of saying nothing. null has no function named get_collider() so it crashes.

That is absolutely awful code so I highly recommend following another tutorial. Check out the Youtube channels: GDQuest, KidsCanCode, Heartbeast, or Game Endeavor to learn how to properly create games in Godot.

timothybrentwood | 2021-05-21 18:50

Try looking in the Godot API for the function calls (such as get_collider()) used in the code.

It looks as though one of the references the above code is using, $Raycast2D2, is null, meaning it isn’t pointing to a node.

Ertain | 2021-05-21 18:56

Oh Wow, I have no clue how it worked for the guy in the tutorial then, anyways thank you for answering and suggesting better tutorials.

AshTheKindra | 2021-05-21 19:30

Ah ok, thank you for answering

AshTheKindra | 2021-05-21 19:31