godot attempt to call function on null instance

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

extends KinematicBody2D

const UP = Vector2(0, -1)
const GRAVITY = 20
const ACCELERATION = 50
const SPEED = 200
const JUMP_HEIGHT = -550

var motion = Vector2()

func _physics_process(delta):
motion.y += GRAVITY
var friction = false

	if Input.is_action_pressed("right"):
		motion.x = min(motion.x+ACCELERATION, SPEED)
		$AnimatedSprite.flip_h = false
		$AnimatedSprite.play("walk")
	elif Input.is_action_pressed("left"):
		motion.x = max(motion.x-ACCELERATION, -SPEED)
		$AnimatedSprite.flip_h = true
		$AnimatedSprite.play("walk")
	else:
			$AnimatedSprite.play("idle")
			friction = true
		
	if is_on_floor():
		if Input.is_action_pressed("up"):
			motion.y = JUMP_HEIGHT
		if friction == true:
			motion.x = lerp(motion.x,0 ,0.2)
	else:
		if motion.y <0:
			$AnimatedSprite.play("jump")
		else:
			$AnimatedSprite.play("fall")
		if friction == true:
			motion.x = lerp(motion.x,0 ,0.5)
	motion = move_and_slide(motion, UP)

whats the prob of my code?
it says that attempt to call function on null instance
on line 24($AnimatedSprite.play(“idle”))

PS: it works fine with i didnt insert the camera 2d
but when i insert the camera2d it just promt a message : attempt to call function on null instance

please help im new in this !

“Null instance” means that Godot couldn’t find the node since it doesn’t exist. “$AnimatedSprite” looks for the node called “AnimatedSprite” that is the direct child of the KinematicBody2D. Do you have a node like that?

exuin | 2020-10-02 04:44

yes i have a node of that, the error happen when i inserted the camera2d as a child in the animationsprite, it works fine when i didnt insert camera2d in the animetedsprite

xtoa | 2020-10-02 05:15

Where did you put the Camera2D? Was it indented more than the AnimatedSprite (as a child)?

exuin | 2020-10-02 05:21

i put camera2d on more than animatedsprite, is there a conflict for putting camera2d on multiple sprite?

xtoa | 2020-10-02 07:03

No, can you post your node layout?

exuin | 2020-10-02 07:12

enter image description here

enter image description here

see the link

xtoa | 2020-10-02 07:20

:bust_in_silhouette: Reply From: exuin

Your problem is that you don’t have a node named “AnimatedSprite”. It’s named “patrick”. You either need to change the paths in your code or change the name of the node to match.

I see, i get it now bro thanks

xtoa | 2020-10-02 10:03

1 Like