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
```