#Can't play animation but there is still a AnimationPlayer node inside

1 messages · Page 1 of 1 (latest)

true spruce
#

Attempt to call function 'play' in base 'null instance' on a null instance.
Idk why it cause this :V

Code:


var SxPSy = scale.x + scale.y
var Speed = SxPSy + 200
var JumpVel = SxPSy * -200

@onready var Anim = $AnimationPlayer

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") * SxPSy

func _physics_process(delta):
    SxPSy = scale.x + scale.y
    scale.x = Global.PSX
    scale.y = Global.PSY
    # Add the gravity.
    if not is_on_floor():
        velocity.y += gravity * delta
    # Handle jump.
    if IIAP("up") and is_on_floor():
        velocity.y = JumpVel
    
    if Global.ResizeMachine == 1:
        if Input.is_action_just_pressed("resize_x"):
            if Global.PSX > 0.5:
                Global.PSX -= 0.1
                Global.Coins += ((0.1 - Global.MinSize) + (0.1 - Global.MinSize)) * 10
                print(Global.Coins)
        if Input.is_action_just_pressed("resize_y"):
            if Global.PSY > 0.5:
                Global.PSY -= 0.1
                Global.Coins += ((0.1 - Global.MinSize) + (0.1 - Global.MinSize)) * 10
                print(Global.Coins)
    # Get the input direction and handle the movement/deceleration.
    # As good practice, you should replace UI actions with custom gameplay actions.
    var direction = Input.get_axis("left", "right")
    if direction:
        velocity.x = direction * Speed
    else:
        velocity.x = move_toward(velocity.x, 0, Speed)
        Anim.play("Idle_Right")
    JumpVel = SxPSy * -200
    gravity = ProjectSettings.get_setting("physics/2d/default_gravity") + (SxPSy + 100)
    Speed = SxPSy + 300
    move_and_slide()


func _on_teleport_portal_body_entered(body):
    get_tree().change_scene_to_file("res://Levels/BattleArea.tscn")

func IIAP(key):
    return Input.is_action_pressed(key)


func _on_area_2d_body_entered(body):
    position = Vector2(-447, 332)```
neat tapir
#

does it always not work or after changing the scene?

neat tapir
#

Which one?

true spruce
# neat tapir Which one?

The game crashed when i run the game and it show "Attempt to call function 'play' in base 'null instance' on a null instance."

neat tapir
#

Does the AnimationPlayer have an animation called "Idle_Right" ?

#

and are you sure this is the script thats causing the error?

true spruce
neat tapir
#

it sounds like the AnimationPlayer is missing

#

I think "func _on_teleport_portal_body_entered(body):" is being called or something else is changing the scene or removing the player from it

#

try replacing:
get_tree().change_scene_to_file("res://Levels/BattleArea.tscn")
with
get_tree().call_deferred("change_scene_to_file","res://Levels/BattleArea.tscn")

#

unless the unloading/deleting/reloading is taking place from somewhere else

true spruce
neat tapir
#

hmm.
try changing this line
@onready var Anim = $AnimationPlayer
to just
var Anim
and put
Anim = $AnimationPlayer
in the _ready() function

neat tapir
#

ok in the _ready function, can you add
print(Anim)
after the Anim = line and see if there is anything outputted?

neat tapir
#

So yeah, godot is saying there is no AnimationPlayer.
Are there any other objects or scripts interacting with the Player?

true spruce
neat tapir
#

Try renaming the AnimationPlayer in both the Player object and in the code

true spruce
#

:V

neat tapir
#

does the print still say the same thing?

true spruce
neat tapir
#

can I see what the other Errors are?

#

I am running out of ideas tbh. It could be something to do with a file not being saved properly, an addon somehow modifying nodes... otherwise I have no idea

#

you can try checking Project > Plugins and then Project > Reload current project

true spruce
true spruce