Area2D not working as intended

Godot Version

Godot 4

Question

Hi! I think my Area2D ain’t working, i wanted to program it so the instances of a scene were deleted when they collided with the Area2D but nothing happens, here’s the code.

func _on_area_2d_area_entered(area: Area2D) -> void:
	remove_child(Global.new_gift)

new_gift is a variable in a Global singleton and i set it to be equivalent to gifts.instantiate(), i called @export var gifts: PackedScene on both the game scebe and the global script even though it is declared as an instance of the gifts scene only in the game scene because i cannot call it globally in the Global script. Here’s the whole game scene’s code:

extends Node

@export var gifts: PackedScene
@onready var sleigh: Sprite2D = $Sleigh
var gift_list = []
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

# Correct function name for the button signal




func _on_button_pressed() -> void:
	Global.new_gift = gifts.instantiate()
	var sleigh_where = sleigh.position
	Global.new_gift.position = sleigh_where
	add_child(Global.new_gift)
	gift_list.append(Global.new_gift)


func _on_area_2d_area_entered(area: Area2D) -> void:
	remove_child(Global.new_gift)

At the moment there is no use for the Array.

Did you check that you connected the area_entered signal to _on_area_2d_area_entered method?
Is this method firing properly when you want it? E.g. put a print("test") statement within the method to see if the method is firing properly.
If it is firing, then you can also double check if Global.new_gift variable holds the proper reference that you expect.

Try to debug and let us know the outcome. It would help if you showed a screenshot of your scene structure.

1 Like

Yeah i checked, everything is connected.
I tried putting a test, nothin’s printed.

I checked if the print works and it does, this is the new code

extends Node

@export var gifts: PackedScene
@onready var sleigh: Sprite2D = $Sleigh
@onready var floor: Area2D = $Floor
var gift_list = []
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	pass

# Correct function name for the button signal




func _on_button_pressed() -> void:
	Global.new_gift = gifts.instantiate()
	var sleigh_where = sleigh.position
	Global.new_gift.position = sleigh_where
	add_child(Global.new_gift)
	gift_list.append(Global.new_gift)
	print("giftin")

func _on_floor_area_entered(area: Area2D) -> void:
	remove_child(Global.new_gift)
	print("test")

I have NO idea of what’s wrong.

Here are the screenshots of the Game scene’s structure

One

Two

For some reason ParallaxLayer2’s child is not shown, it is just another Sprite2D though

Is it the Sleigh that is supposed to enter?
Does that scene have an Area 2D?

Is the gift a RigidBody2D? Because then you need to use the body_entered, not area_entered signal.

2 Likes

It is not, sleigh is a sprite (a sleigh) and the gift scene instances just kinda appear on its position on the press of a button.

Yeah it is a RigidBody2D, thanks for the help! When I’m home I’ll check if that was the only problem.

I can officially confirm that was the problem, even though now there is another one!

as you can see if I drop one gift all’s fine, but if two are dropped one won’t disappear! then they’ll stack up.

It’s because you are removing the last created gift, not the one that hit the floor.

remove_child(Global.new_gift)

You should instead remove the gift that hit the floor. It’s right there, provided to you by the signal. I would also use queue_free() instead of remove_child(), unless you have a specific reason to use the latter. Your script should look something like that now:

func _on_floor_body_entered(body: Node2D) -> void:
	body.queue_free()

Why do you even store the gift in the autoload Global.new_gift? Seems completely unnecessary to me.

1 Like

Thanks I didn’t know I could do body.queue_free

btw Yeah I recently realized it was truly unnecessary to store new_gift on Global, I did it when I forgot that I could call globally a variable (I needed the new_gift variable global to call it in different scopes) and then define the variable inside a scope. (I hope you understand what I mean)
I know it’s the dumbest thing I could forget, because I anyways defined a variable that was declared in Global in a scope.

Hope that all works for you now.
If you have no further issues - mark my previous response as a solution, so that others know the issue is solved in case they ever encounter a similar problem.

1 Like

Yeah, I’ll check if everything works and if it does I’ll mark the response as a solution.

2 Likes