What is wrong with this animation player

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

I have a Control node as a parent and it has 3 children(Sprite, AnimationPlayer, Node).

This is the code:

func _ready():

happy()

func happy():

$AnimationPlayer.play(“Happy”)

For some reason this doesn’t work, gives error : “Attempt to call function ‘play’ in base ‘null instance’ on a null instance.”

I tried putting animation player on an onready var but same result. I tried using get_child(1) but it says children count is 0 even tho I’m sure it has 3 children. What can be the problem?

:bust_in_silhouette: Reply From: jgodfrey

Which node is the script associated with? For the above to work, it needs to be on the Control node (as you’ve described it above). I assume it’s not. Can you show your scene tree?

AIFace(Control node)

Sprite(Sprite)
AnimationPlayer(AnimationPlayer)
Voices(Node)

srry can’t think of any other way of showing it

oh also script is attached to AIFace

MemokingMH | 2022-08-14 15:02

Hi, I’m interested in this because I can’t think of a way to make it not work unless there are different names on your nodes.
I imagine that you used the () to indicate the type of the node and they are not part of its name. If not, there is the error.
You can use print_tree_pretty() to print your node tree to the console and paste it here.
In the debugger on the errors tab you should have a more detailed description of the error.
Your script on the main node extends Control?
I can not think of anything else…

estebanmolca | 2022-08-14 15:45

srry can’t think of any other way of showing it

I meant for you to post an image. And, instead of showing us small snippets of what’s in your code, just post the code itself. I think there’s probably a difference between what you’re trying to describe here and what you actually have (that you probably don’t realize).

Posting the actual code and an image of your scene tree will help to resolve any differences.

jgodfrey | 2022-08-14 16:17

Here is the scene tree
┖╴AiFace
┖╴AIFace
┠╴AnimationPlayer
┠╴Sprite
┖╴VoiceLines

also don’t know if this helps but when I try to run a non existing animation it works(well it doesn’t play animation but it doesn’t crash)

Here is the full code:

onready var anim_player = get_node(“AnimationPlayer”)
var currentFace : String = “Happy”

func _ready():

happy()

func happy():

anim_player.play(“Happy”)

func sad():

anim_player.play(“Sad”)

func surprised():

anim_player.play(“Surprised”)

MemokingMH | 2022-08-14 19:00

We’re missing some detail here that’s not being reflected in what you’re still posting. The above doesn’t really help because:

  • The code is not formatted as code for the forum. That loses context that’s potentially VERY important for finding code-based issues. Please edit your above message and format the code for the forum. To do that, select the code block and press the { } button in the forum editor’s toolbar. Then, check the Preview window to verify proper formatting.
  • The reason I asked for a screenshot of your scene tree is to ensure that you’re not making a mistake when manually reproducing it in your message.

What you’re trying to do should be very simple, and there can only be a few things wrong (generally, scene tree organization, node names, or script associated with the wrong node). With properly formatted code and an image of your scene tree, it’s likely that the issue can be spotted.

jgodfrey | 2022-08-14 19:31