Area2D click detection and instancing scenes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MaybeAnonymous

Allow me to explain my issue, I am using an Area2D node as something that the player has to click on to earn points, and I would like to know how I could make the click detection work for all nodes in a group, I’ve added my Area2D node to a group, and I have to instance it. Nothing happens when I click the instances, only when I click the original node.

extends Node2D

var score = 0

onready var clickme = preload("res://assets/ClickMe.tscn")
onready var clickmeinstance = clickme.instance()

func _on_ClickMe_input_event(_viewport, _event, _shape_idx):
	if Input.is_mouse_button_pressed(1):
		clickmeinstance.position = Vector2(rand_range(0, 320), rand_range(0, 240)).round()
		get_tree().get_root().call_deferred("add_child", clickmeinstance)
		get_node("CanvasLayer/Score/ScoreLabel").text = str(score)

is the collision polygon present in the instanced areas?
you can check by running the project with visible collision shapes enable (top screen, debug=>visible collision shape)

Andrea | 2021-08-29 11:02

I just checked, the collision Polygon is indeed present.

MaybeAnonymous | 2021-08-29 13:08

Is it possible the problem lies on adding the new instance as child of the root?
Also, just to exclude there is nothing wrong with the scene, does it work if you instance it in a less fancy way? (Eg: create many instances on the ready of the parent, through a for loop)

Andrea | 2021-08-29 14:29

I don’t know much about for loops and my game needs to instance the node when I click the one that came before

MaybeAnonymous | 2021-08-29 17:16