0 votes

I instance Bullets. Then I want that a enemy prints HIT if a bullet hits him. This I want to do with

func _on_Area2d_body_entered(body):
         if body.get_name == "Bullet"
             print("HIT")

But the problem is, that the name of the Instanced Bullet is something like @Bullet@17 and not just Bullet. How can I fix that?

in Engine by (325 points)

1 Answer

+1 vote
Best answer

A better way to do this is to use groups. When you create a bullet, add it to a group called bullets by doing something like:

bullet.add_to_group("bullets")

or by having the add_to_group("bullets") line in the _ready() function of the bullets script.

Then change your code to something like

func _on_Area2d_body_entered(body):
     if body.is_in_group("bullets"):
         print("HIT")

You can read more about groups here:
https://docs.godotengine.org/en/3.1/getting_started/step_by_step/scripting_continued.html#groups

by (436 points)
selected by

Thank you very much!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.