How to teleport a player from one object to another

Godot Version

4.2.1

Question

Hi I’m new to Godot and I’m trying to implement a feature that allows the player to teleport from one object to another object once they touch it. My issue is is that I keep getting a null error that states the object doesn’t exist even though I already added it to the scene. I don’t know if this is a hierarchy issue or something like that but I’ve been trying everything and I don’t know what’s wrong.

Here’s the code for the object the player teleports from

Is node “Refrac” a child node of Area2D this code is attached to?
Sharing a scene tree would help.

When you use get node you should also use @onready

When the script is loaded, the scene is not completly loaded, hence the null exception. You solve this by either setting the reference in _ready() - function or using the handy @onready-annotation as mentioned above by @shatteredreality

@onready var refrac_position = get_node("Refrac").position

or

var refrac_position: Vector2 = Vector2.ZERO
func _ready():
   refrac_position = get_node("Refrac").position

Docs: @onready

Also depending on your scene tree you most likely want to use the global_position. As the “position” itself is relativ to the parent node.

It is not here is the scene tree for the level

image

Ok, you probably still end up in null then. Have a look here for more info regarding node paths:

kidscancode: getting_nodes

[quote=“J.G.S, post:4, topic:46696”]
@onready var refrac_position = get_node("Refrac").position
[/quote] Thank you I got it working now, just need to tweak collision a bit