Node/object disapears when changing scene

Godot Version

4.1.1

Question

I don’t know why but a sprite2d node in my scene disappears when I change and return to that scene.
video of the issue->

Before
12
After
11

door entrance

extends Area2D

var player = null
func _on_body_entered(body):
	player=body
	var scene = preload("res://scenes/workshop_inside.tscn").instantiate()
	get_tree().change_scene_to_file("res://scenes/workshop_inside.tscn")

func _on_body_exited(body):
	if (body == player):
		player = null

exit

extends Area2D
var player = null
func _on_body_entered(body):
	player=body
	var scene = preload("res://scenes/node_2d.tscn").instantiate()
	get_tree().change_scene_to_file("res://scenes/node_2d.tscn")


func _on_body_exited(body):
	if (body == player):
		player = null

Your screenshots do not show the same scene tree; one shows the Local tree and the other shows the Remote tree. Is your problem that the object disappears when you’re starting the game?

As far as I can tell from your screenshots, the node that is disappearing is Sprite2D.
What is this node?

Yes the screenshots show the local and remote tree. it shows that the sprite2D gets removed when I change scenes. The video I posted showed the issue happening ingame.

1 Like

Aah, that’s my mistake. I thought the link was to your project files.

I still want to stress that the local scene tree is a representation of how the scene is set up prior to running the game. I don’t see how that’s relevant to your issue which happens at run-time.

Question

Can you explain what you’re doing in the lines below? It doesn’t make sense to me:

var scene = preload("res://scenes/node_2d.tscn").instantiate()
get_tree().change_scene_to_file("res://scenes/node_2d.tscn")

here I changed the scene

Listen, you cannot expect to have your issue debugged by other people when the information you’re providing is loaded with errors and misdirection.

You don’t seem to understand the separate elements in your code, nor do you understand what the difference between the Local and Remote scene tree is.
Clearly you are changing the scene - just not correctly.

To help you understand your own work, I need you to describe what the 1st and 2nd line does. Describe it in detail.

Line 1:

var scene = preload("res://scenes/node_2d.tscn").instantiate()

Line 2:

get_tree().change_scene_to_file("res://scenes/node_2d.tscn")

If you’re unable to do so, and still want to have your problem fixed, I strongly recommend that you show all relevant code. Only then will forum users be able to reliably help you solve your problem.

You have also yet to answer my initial question:

…the node that is disappearing is Sprite2D. What is this node?

1:I preload the scene (node2d) and instantiated it in the tree.
2: then I changed the scene to the previously mentioned node2d. tscn

the sprite2D node is a node with Sprite2d root node with some an area2d and collisions as its children. The sprite2D has an Area2d node where when the player collides with it the scene changes.

You are close to being correct.

Line 1

The thing you are misunderstanding is that calling instantiate() only creates an instance of the scene - it doesn’t automatically add it to the scene tree (for that you would use add_child()).

What this line is doing right now is: making an instance of the scene located at the path ("res://scenes/node_2d.tscn") and then storing that instance in a variable called scene. You are not using scene anywhere, so there’s no reason to do any of it.

Line 2

You are mostly correct. You are changing the scene via get_tree().change_scene_to_file(). In your case, the function changes the scene to a scene that is located at the path you’re giving it ("res://scenes/node_2d.tscn"). This has nothing to do with line 1.

Line 1 & 2 summary

If you delete “line 1”, your code still works as it did before because “line 1” doesn’t achieve anything.

Request

Could you please provide an image of the scene tree content within the two scene files?:

  • node_2d.tscn
  • workshop_inside.tscn


deleting line 1 from both scripts fixed the issue

extends Area2D
var player = null
func _on_body_entered(body):
	player=body
	get_tree().change_scene_to_file("res://scenes/node_2d.tscn")

func _on_body_exited(body):
	if (body == player):
		player = null
1 Like

Happy to hear it. I’m not sure why though.