cannot get collision information using move_and_collide()

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

I am trying to rotate a kinematic body and then check if the body is colliding using the move_and_collide() function, with ‘test_only’ as false. However, whenever i attempt to get collision information, it returns an ‘invalid get index’ or ‘null instance’ error (depending on how i’m getting this information)

Here is the code:

func _physics_process(delta):
    CNTRL() #does the movements and stuff
    var testcol = move_and_collide(Vector3(0, 0, 0.1), false, false, true)
    if testcol.testcol.collider().get_collider_id() == "world": # if colliding with the object (in theory)
        print("colliding") #return

From what i’ve read everywhere else, i’m not doing anything wrong. Wondering if maybe i’ve messed up some syntax? Used .collider() and .collider so i dont think its that though.

:bust_in_silhouette: Reply From: kidscancode

move_and_collide() returns a KinematicCollision object if there’s a collision. When you move and there’s no collision, it returns null, which is exactly what your error says.

As explained here:

You must check if there even was a collision before trying to access properties of that collision.

There are other problems with your code:

  • You wrote testcol.testcol and the KinematicCollision doesn’t have a testcol property
  • collider is a property of KinematicCollision, not a function
  • collider_id is also a property, but it’s an int, and so could never be equal to “world”

This could work (not knowing exactly what you’re trying to check):

if testcol and == "world"