Script in Instance of Scene not getting executed

Godot Version

4.0

Question

Hi
I have a simple Script that i attached to my “Banana” 2dArea.

The “Collection” of the banana runs smoothly and 2 new bananas are spawned. but it seems that in the new instances the script is not executed.
I would expect “banana ready” to be printed if the bananas are instanciated.
When I instanciate a scene, do the scripts not also get instanciated?

extends Area2D

# Called when the node enters the scene tree for the first time.
var banana_scene = preload("res://scene/banana.tscn")
var rng = RandomNumberGenerator.new()

# get screen size
var screen_size = get_viewport_rect().size

@onready var anim = $BananaSprite

func _on_body_entered(body:Node2D):
	print("body entered")
	if body.name == "CharacterBody2D":
		anim.play("collect")
		for i in range(2):
			var new_banana = banana_scene.instantiate()
			new_banana.position = Vector2(rng.randf_range(0, 1000), rng.randf_range(0, 1000))
			get_parent().add_child(new_banana)
			print(get_parent().name)
		await anim.animation_looped
		print("banana collected") 

func _on_banana_sprite_animation_looped():
	if anim.animation == "collect":
		queue_free()

func _ready():
	print("banana ready")

You have errors showing.
First you should address whatever errors those are.

You have this in your banana scene code:
var banana_scene = preload("res://scene/banana.tscn")

This preloads the banana scene within the banana scene. Off hand I can’t think of any reason this wouldn’t work but I would suggest there is a design flaw here.
I think it would be better for the parent to create its own children rather than the children create siblings.

I would print the value of new_banana to see if it is legit and then
print(new_banana.anim.animation)
as maybe it is queue_free()'d before _ready() runs (although I would not think this possible).

I tested this with a simpler setup and got a cyclic resource inclusion error. You shouldn’t load the scene inside itself.