Error with .play

Godot Version

4.2.2

Question

I set up a lin of code for my animations to play using this code

	if Input.is_action_just_pressed("quit"):
		get_tree().quit()
	if Input.is_action_just_pressed("retry"):
		get_tree().reload_current_scene()

	if Input.is_action_just_pressed("forward"):
		AniSprite.play("Fwalk")
	if Input.is_action_just_pressed("back"):
		AniSprite.play("Bwalk")
		```

this worked for a day before showing the error:
Attempt to call function.play in base null instance on a null instance.

any reason for the random error. it did this before and randomly worked. 
any advice on whats happening why the random error then fixing itself just for it to break again


EDIT fulll script 
```extends CharacterBody3D

@onready var AniSprite = $"CanvasLayer/carcter UI/AnimatedSprite2D"
@onready var Raycast = $RayCast3D


const SPEED = 5.0
const JUMP_VELOCITY = 4.5
const MOUSE_SENS = 0.5 


var dead = false 

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	
func _input(event):
	if dead:
		return
	if event is InputEventMouseMotion:
		rotation_degrees.y -= event.relative.x * MOUSE_SENS
			

		
		
	
func _process(delta):
	if Input.is_action_just_pressed("quit"):
		get_tree().quit()
	if Input.is_action_just_pressed("retry"):
		get_tree().reload_current_scene()

	if Input.is_action_just_pressed("forward"):
		AniSprite.play("Fwalk")
	if Input.is_action_just_pressed("back"):
		AniSprite.play("Bwalk")
		

	if dead:
		return
	
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")



func _physics_process(delta):
	if dead:
		return
	
	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta
		


	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir = Input.get_vector("left", "right", "forward", "back")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	move_and_slide()

func _retry():
	pass
	
	
func _move_and_slide():
	pass
	```

What is AniSprite? A variable that is defined inside the script? A Unique Access name?. The script doesn’t know what it is.

2 Likes

the scripte knows i have it in my onready its refrencing my Animated 2D Sprite looks like this

@onready var AniSprite = $"CanvasLayer/carcter UI/AnimatedSprite2D"

Could I ask you to verify the path? Maybe even delete the $path and drag’n drop the node again.
Or make use of the Unique Access and use that instead of @onready in case it doesnt work.

not sure how to go about that can you explain little more please

right now I believe if you try to print out
print(AniSprite) → this will return null because it cannot find the node under $"CanvasLayer/carcter UI/AnimatedSprite2D".
Is this path written correctly? Did you perhaps rename any of the nodes?

If all objects are in the SceneTree, you could rightclick on the AnimationSprite2d and then Access as Unique Name, then you can drag and drop the node into the script.


It will most likely turn the code into this:

	if Input.is_action_just_pressed("forward"):
		%AnimatedSprite2D.play("Fwalk")
	if Input.is_action_just_pressed("back"):
		%AnimatedSprite2D.play("Bwalk")
1 Like

Thanks i appreciate the comment this helped solve it.

is there any reason why dragging and dropping the namewouldnt work? i just wanna know for my own sake tbh its okay id you don’t wanna explain

I am not sure why your @onready did not work.
I assume you are dragging the node into your script while holding the CTRL key in order to autocreate your @onready.
Many beginners write out the nodepath themselves and then accidentally mistype.
Or they did it correctly but then renamed the nodes and forgot to adjust it in the script.
The @onready can also fail if the node is not ready while the script is executing. There are several causes so it can become quite confusing.

1 Like

ah okay thanks I appreciate your help

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.