Godot Version
4.5
Question
Why do I get this error:
E 0:00:02:441 nextlevel.gd:12 @ _on_body_entered(): Parameter “data.tree” is null.
<C++ Source> scene/main/node.h:507 @ get_tree()
nextlevel.gd:12 @ _on_body_entered()
for this code:
extends Area2D
func _on_body_entered(body: Node2D) → void:
var current_scene_file = get_tree().current_scene.scene_file_path
var next_level_number = current_scene_file.to_int() + 1
var next_level_path = "res://levels/level" + str(next_level_number) + “.tscn”
print(next_level_number)
get_tree().change_scene_to_file(next_level_path)
this is my scene thingy:
Is that the full script? Is that the correct script? You error points to a line 12, but what you’ve pasted is only 9 lines long.
It could be that multiple objects are overlapping this area on a single frame, thus trying to change scene multiple times while the tree is invalid. Ensure only one call to change_scene_to_file happens per-frame.
1 Like
its the var current_scene_file = get_tree().current_scene.scene_file_path
i used this video https://www.youtube.com/watch?v=GZrALMvOwY8
Could I just have a variable that checks if it happens then the next time it tries it it blocks the second one?
You get that error because your node is not in scene tree. You need to check your logic there and we cannot help with that with the limited information you provided.
But if you want to avoid the error you can first check if the node is inside the scene tree by using the function is_inside_tree() which returns a bool. Something like this:
func _on_body_entered(body: Node2D) → void:
if is_inside_tree():
var current_scene_file = get_tree().current_scene.scene_file_path
var next_level_number = current_scene_file.to_int() + 1
var next_level_path = "res://levels/level" + str(next_level_number) + “.tscn”
print(next_level_number)
get_tree().change_scene_to_file(next_level_path)
1 Like
I fixed it by doing something I forgot but I will send what I did later