0

This is very stupid but I can't seem to fix it. The enemy is randomly instance in the scene so I attached a script to the main enemy scene that has this code:

func _on_Enemy_body_entered(body):
    if is_in_group("bullet"):
        Player.score += 1
        queue_free()

The bullet scene which is also instanced to the main scene when the player shoots is in a group called "bullet".

Summon
  • 77
  • 4

1 Answers1

0

First of all, the usual debugging approaches apply:

  • Add print statements or breakpoints to see the value of variables at a given point (or if that executes at all).
  • Enable "Visible Collision Shapes" from the debug menu to see if the colliders are how you expect during execution.
  • Select the nodes on the remote tab of the scene panel when the game is running to see the values of properties in real time in the inspector.

Given the code, I expect you to find that the code inside the if statement is not executing at all.

func _on_Enemy_body_entered(body):
    if is_in_group("bullet"):
        Player.score += 1
        queue_free()

You say the code is in the enemy. Is the enemy is a group called "bullet"? I'm going to guess it isn't. So is_in_group("bullet") evaluates to false, and thus the execution flow does not enter the if statement.

To be clear, if is_in_group("bullet"): is the same as if self.is_in_group("bullet"):. It is calling is_in_group on the object on which the script is attached.


Since you mention the bullet is in a group called "bullet", I'm also going to guess you want to check the body that collided the enemy. That is:

func _on_Enemy_body_entered(body):
    if body.is_in_group("bullet"):
        Player.score += 1
        queue_free()

That should be better.

I don't see you delete bullet, but I can't tell if that is intended or not.

There are the usual consideration to detect collisions. So it might still not work. You can see elsewhere for a full explanation. For the short version: check if monitoring is enabled (and contacts reported is large enough on rigid bodies), and check the collision layers and masks overlap, and that the collision shapes are not disabled, and of course check that the signals are connected.

Theraot
  • 31,890
  • 5
  • 57
  • 86