Cannot show control a node's visibility after switching scenes

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

I am developing a game which has moving obstacles controlled by the tween node. They have collision boxes, and detect collisions with the player. I have a scene which has the obstacles and the player, but I also have another scene which is the level menu. When I click on a level, it should take me to a scene which contains the level requested. I have done all that already, but I have a problem. If I run the level scene directly and test collision, all works well. But when I run the level through the menu and test collision, when I hit the obstacle, the game instantly crashes with an error:

E 0:00:05.818   get_node: (Node not found: "/root/Node2D/DiePanel" (absolute path attempted from "/root/@Node2D@2/StaticBody2D").)
  <C++ Error>   Condition "!node" is true. Returned: nullptr
  <C++ Source>  scene/main/node.cpp:1322 @ get_node()
  <Stack Trace> @ _on_Area2D_area_entered()

This is my code for the current only moving obstacle in the game:

# extends StaticBody2D
extends Area2D
func _ready():
	$Tween.interpolate_property(self, "position", Vector2(0, 0), Vector2(100, 0), 1, Tween.TRANS_BACK, Tween.EASE_IN_OUT)	

func _on_Tween_tween_all_completed():
	$Tween2.interpolate_property(self, "position", Vector2(100, 0), Vector2(100, -100), 1, Tween.TRANS_BACK, Tween.EASE_IN_OUT)	

func _on_Tween2_tween_all_completed():
	$Tween3.interpolate_property(self, "position", Vector2(100, -100), Vector2(0, -100), 1, Tween.TRANS_BACK, Tween.EASE_IN_OUT)	

func _on_Tween3_tween_all_completed():
	$Tween4.interpolate_property(self, "position", Vector2(0, -100), Vector2(0, 0), 1, Tween.TRANS_BACK, Tween.EASE_IN_OUT)	

func _on_Area2D_area_entered(area):
	var DiePanel = get_node("/root/Node2D/DiePanel")
	get_tree().paused = true


:bust_in_silhouette: Reply From: ponponyaya

You say it was ok when you run the level scene directly, but have error when you run the level through the menu.

It looks like your absolute path is wrong when you run the level through the menu.

I don’t know what is your scene tree structure, but here are some tips.
Try not to use absolute path( since it may change when you use code to instance your scene), try to use the default variable area in function _on_Area2D_area_entered(area).
Anyway, if you need, you can make a custom signal or pass your custom variable to solve some problem.

That is what I did.

Edit: It turns out I have to use the method: get_tree.change_scene() to change scenes without errors in collisions.

RockZombie4 | 2022-02-28 01:03

You may give more information, for example the scene tree structure image or other informtion, so that I can give you more help. In fact, I don’t know where your DiePanel and other nodes are(I mean the relationship of scene tree structure)?

ponponyaya | 2022-02-28 01:54