I am following a tutorial to change between scenes in Godot 4 and getting an error that wasn’t covered in the video because it didn’t happen. The trigger worked before I tried to add a scene manager and now the game crashes when the player character enters the trigger. The error message in the title of this post.
here are both of the scripts:
class_name SceneTrigger extends Area2D
@export var connected_scene: String #name of scene to change to
func _on_body_entered(body):
if body is Player:
scene_manager.change_scene(get_owner(), connected_scene)
class_name SceneManager extends Node
var player: Player
var scene_dir_path = “res://scenes/”
func change_scene(from, to_scene_name: String) → void:
player = from.player
player.get_parent().remove_child(player)
var full_path = scene_dir_path + to_scene_name + ".tscn"
from.get_tree().call_deferred("change_scene_to_file", full_path)
I tried adding my Player scene to the ‘player’ group but that didn’t seem to change anything. sorry if this is a noob question, I am a complete beginner and code in my spare time.
If you get an error like this, then it means that you’re trying to get a variable (in this case: player
) from a script that (in this case) extends Node2D
and simply doesn’t contain this variable. You’ll also get a line number that points you the root of the problem:
func change_scene(from, to_scene_name: String) → void:
player = from.player # <-- here the error occurs
Now you know that your change_scene
function is called at some point with its first argument being a Node2D
that does not have a variable called player
. So we go on the hunt for any calls to this method et voilla:
func _on_body_entered(body):
if body is Player:
scene_manager.change_scene(get_owner(), connected_scene)
Here you’re passing the result of get_owner()
to your function, which can be any ancestor of your Area2D (or null
if it isn’t set). From the look of your code you probably want to pass body.get_parent()
here, although (with a few minor changes in your function) body
would also work.
PS: When posting code in this forum, click the “</>” button in the editor to format it properly. Preserves the indentation, thus makes it easier to read and increases your odds of getting help. 
thank you for your response. I have been messing with it and my scene trigger script now looks like this:
class_name SceneTrigger extends Area2D
@export var connected_scene: String #name of scene to change to
var player
func _on_body_entered(body):
if body is Player:
scene_manager.change_scene(player.get_parent, connected_scene)
The game now runs but crashes when I walk into the trigger. The editor tells me the break is in the func _on_body_entered part of my code with the error being Invalid get index ‘get_parent’ (on base: ‘Nil’). what does that mean?
That you’re trying to access a variable called get_parent
on a node that doesn’t exist. The latter is because you only declared player
, but never assigned a value. The former is because you actually wanted to call a function with that name, not a variable, but simply forget the brackets in the end. So instead of this:
scene_manager.change_scene(player.get_parent, connected_scene)
you have to do this:
scene_manager.change_scene(body.get_parent(), connected_scene)
1 Like
Ah! I knew it was something simple that I was missing. I will try this when I get home later today. Thank you so much!