Attempt to call function 'get' in base 'null instance' on a null instance. (Teleportation)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HarryCourt
:warning: Old Version Published before Godot 3 was released.

I have this teleport node that the player will collide with, that will hopefully make change the position of the player. But everytime I run the game, when it collides with the node, it crashes! Can someone help me please?

extends KinematicBody2D

#const GRAVITY = 200.0
const WALK_SPEED = 1200
var MyPos = self.get_pos()
#var teleportTile = get_node("Teleport1").get("teleportPos") The code where everything changed...

var velocity = Vector2()
func _ready():
    set_fixed_process(true)

func _fixed_process(delta):

	var motion = velocity * delta
	move(motion)
	if is_colliding():
		var collider = get_collider()
		if collider.is_in_group("Tile"):
			get_node("SamplePlayer").play("TileOn2")
			collider.queue_free()
		elif collider.is_in_group("Teleporter"):
			set_global_pos(Vector2(get_node("Teleport1").get("teleportPos")))
			get_node("SamplePlayer").play("Finish")
			collider.queue_free()
	
	velocity.x += delta #* GRAVITY
	velocity.y += delta #* GRAVITY
	
	if (Input.is_action_pressed("ui_left")):
	    velocity.x = -WALK_SPEED
	elif (Input.is_action_pressed("ui_right")):
	    velocity.x =  WALK_SPEED
	elif (Input.is_action_pressed("ui_down")):
	    velocity.y = WALK_SPEED
	elif (Input.is_action_pressed("ui_up")):
	    velocity.y = -WALK_SPEED
	else:
	    velocity.x = 0
	    velocity.y = 0
:bust_in_silhouette: Reply From: kidscancode

Any time you get the error “Attempt to call function XXX in base ‘null instance’ on a null instance” it means you have tried to get_node a node that doesn’t exist.

get_node("Teleport1").get("teleportPos")

Is there a node called “Teleport1” that’s a child of this node (running the script)?

No, it’s not a child of the script.

HarryCourt | 2017-08-27 04:05

:bust_in_silhouette: Reply From: Griefchief

You can travel through nodes with directory traversal syntax

get_node("../../Teleport1") 

This isn’t your exact position, figure yours out by looking at the node tree and guessing from there…

Bellow targets it from root node of the current main scene:

get_node("root/something/Teleport1")

You probably know this already, but if you don’t, learn to use print and the debugger to debug code, it will help you with solving this.

This answer has more details on this, to not repeat myself:
https://forum.godotengine.org/2476/can-a-node-under-a-instanced-scene-call-get_node-by-path

P.S. unfortunately I can’t edit other users questions at my level of trust currently, so it was better to add this as a second answer. But feel free to mark the first persons answer as “correct” if mine help you solve it, cause it was inspired by his… I will not shy from a +1 if that helps you, i need some trust on this website :wink:

Griefchief | 2017-08-27 05:00

just noticed one other thing: your commented out “#var teleportTile” line near the start has to be inside of _ready when uncommented, because initialization of other nodes is only guaranteed in _ready if I understand gdscript correctly.

Griefchief | 2017-08-27 05:06