Is_In_Group not working

Godot Version

4.6

Question

im trying to make bullets disappear when they touch the enemy, but they aren’t. am i doing something wrong? here’s my code

func _on_pb_hitbox_body_entered(body) -> void:
	if body.is_in_group("Enemy"):
		print("it work!!!")
		queue_free()
	else:
		pass

i dont know whats wrong

Read this:

Then give us enough information to help you. How is it not working? How does it fail?

i already said i have no idea, and i already searched it to no results

i know the function works, its the if body.is_in_group that doesn’t work somehow

i’m 90% sure the syntax on the group is right

heres the full code if it helps at all

extends Node2D

var dir=Vector2(1,0)

@export var bullet_speed = 900
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	self.position += dir * delta * bullet_speed


func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
	queue_free()


func _on_pb_hitbox_body_entered(body) -> void:
	if body.is_in_group("Enemy"):
		print("it work!!!")
		queue_free()
	else:
		pass

How do you put the enemy into group “Enemy”?

group tab next to inspector. i think it should work, both the scene root and the area2d are in the group

This is a very important question. Does your “it work!!!” print? How are you sure the function is being called?

Group names in the editor may be capitalized automatically but the group needs to follow the underlying name correctly, you can hover over the assigned group to see if it was really capitalized or not.

Can you share a screenshot of your enemy scene tree perhaps?

the “it work!!” does not print, this is why i believe its a problem with the group being called

i’m fairly certain it’s also nothing to do with collision masking, because i haven’t touched collision masking at all, and the bullets ARE HITTING the enemy, it’s just a problem with the player bullets dissappearing

here are the scene trees you asked for

Your enemy appears to be a Node2D which does not have a body to collide with. Maybe you mean to use the Area2D as collision, that would require you to connect to the area_entered signal instead of body_entered.

What you’ve programmed will delete the bullet on contact with any enemy, if this isn’t your intent then it’s a great example of why you must always explain what really happens and what you expected to happen.

“Not working” doesn’t let us know either part of the equation, like saying “my math is wrong I keep getting 3” doesn’t tell us what number you are expecting to get and what equation results in 3 instead of ??.

that’s what i WANT to happen, but the bullet isn’t deleting when i hit the enemy. i tried using the area_entered signal in the playerbullet script (is that the problem? should i be putting the bullet deletion in the enemy script instead?)

sorry for not giving enough info initially, i was really tired and needed sleep

This information was missing in your initial post. In the link I posted, number 2 under Describe Your Problem reads:

It may not be obvious to your readers what you think should happen, so tell us. Don’t assume that the reader has played the same games as you or watched the same tutorials. Don’t expect anyone to watch a 30 minute tutorial video to see where you went wrong. When a video is integral to the problem, make sure to specify which exact part of the video is relevant, e.g. “from 1m20s to 2m30s”.

You answered this question with:

We understand that you don’t know why the problem exists. we want to know what the problem is so we can help you. I didn’t link that to be a jerk, but to help you. You clearly did not read it.

This was the answer to my question.

No problem.


There are a number of problems in your code. One is you’re waiting for a body_entered signal and you have no PhysicsBody nodes in your scenes. So you need to connect to the area_entered signal. The other is that you’re adding the Enemy node to the group, and then testing the Area2D that is hit to see if it is an Enemy. So I modified your code. It should work now.

extends Node2D

var dir := Vector2(1,0)

@export var bullet_speed = 900

@onready var pb_hitbox: Area2D = $PBHitbox

func _ready() -> void:
	pb_hitbox.area_entered.connect(_on_area_entered)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	self.position += dir * delta * bullet_speed


func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
	queue_free()


func _on_area_entered(area: Area2D) -> void:
	if area.get_parent().name.contains("Enemy"):
		print("Enemy Hit!")
		queue_free()

thank you so much! it works exactly like i wanted it to!

and yeah i have to admit i was a bit tired my brain probably thought you were just trying to be rude, and i most definitely did NOT read the link you sent. sorry again for not giving what i wanted to happen and what was happening, and not listening to your advice the first time. and again, thank you so much for helping!

No problem. We’re here to help. Just keep in mind we are people who volunteer our time for free.

Do us a favor and since that worked, mark my previous post as the solution to your problem.

i was about to! i kinda forgot lol

actually, there’s an issue, and i’m not sure if i should make a new post for this or anything, but the bullets delete only when colliding with the FIRST enemy (and the next enemy to spawn once the first one dies or goes offscreen)

i want this to happen with all enemies, tanky or weak, which is why i tried to use groups initially.

i haven’t altered any code since updating it with yours. it’s still hitting the enemies, and dealing damage, but the bullets no longer delete on these second enemies. there are no error messages, and i haven’t changed anything in the editor.

Yeah that makes sense. We can fix that by checking the group now.

extends Node2D

var dir := Vector2(1,0)

@export var bullet_speed = 900

@onready var pb_hitbox: Area2D = $PBHitbox

func _ready() -> void:
	pb_hitbox.area_entered.connect(_on_area_entered)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	self.position += dir * delta * bullet_speed


func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
	queue_free()


func _on_area_entered(area: Area2D) -> void:
	if area.get_parent().is_in_group("Enemy"):
		print("Enemy Hit!")
		queue_free()

Try that. If it doesn’t work, then check the capitalization of your group named “Enemy”.

that works! i stress tested it with like 50 enemies onscreen, and it works! thank you so much!

You’re welcome. Glad you got it working. If you have any questions about why it works, let us know.