I'm making a 2D platformer, and am starting to create what happens in my Enemy's script when it collides with the Player. Both characters are KinematicBody2D, and the enemy's movement is handled using move_and_slide(). Here's the relevant code so far...
if get_slide_count() > 0:
for i in get_slide_count():
var collision = get_slide_collision(i)
if collision.collider.name == "Player" and hit_player == false:
hit_player = true
$HitSound.play()
yield(get_tree().create_timer(1.0), "timeout")
hit_player = false
At the moment, all I want it to do is play a sound, then wait for 1 second before resetting and checking for collisions again. What's happening is that after that 1 second pause, the game stops, and I get:
"Invalid get index 'collider' (on base: 'null instance')"
...and the highlighted line is:
if collision.collider.name == "Player" and hit_player == false:
Any idea what's causing this? I've tried checking to make sure get_slide_collision is not null before doing the slide count, I've added the "if get_slide_count() > 0" line at the top to avoid a null slide count... but those are not the issue, apparently.
Thanks in advance for any light you can shed!