My instantiated object dont work after changing the scene

I have a object called “hole”, that when you touch it and press “E” you go to random scene, but when you are at this other scene, the hole at this the random scene dont appears and if i instantiate the hole at the random scene it just dont work

Here my hole script:

extends CharacterBody2D

var inArea : bool = false
@onready var player = get_parent().get_node("Player")
var ChooseRooms: Array = [preload("res://Rooms/room_1.tscn"),
preload("res://Rooms/room_2.tscn")]

func _on_enter_area_body_entered(body):
if body.is_in_group("Player"):   
	inArea = true
	$CanvasLayer/InteractionText.visible = true

func _on_enter_area_body_exited(body):
  if body.is_in_group("Player"):
	inArea = false
	$CanvasLayer/InteractionText.visible = false

func _process(delta):
  if Input.is_action_just_pressed("interaction_key") and inArea == true:
	var roomChoosed = ChooseRooms[randi() % ChooseRooms.size()]
	get_tree().change_scene_to_packed(roomChoosed)

Please help us help you. Format your code with the </> tool. Explain what this ‘hole’ is? Maybe a screenshot of your node tree.

Imagine that we can’t read your mind :blush:

1 Like

this thing, means deleting whole scene, and load the new one. You will want to save it (the hole) to global variable(use autoload) and respawn (instantiate it again) on new level/room

other way is to queue free and load scene yourself with a main controller which doesnt use this change_scene function. this doesnt delete whole nodes even with the script in main scene tree