0 votes

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?

Godot version latest
in Engine by (70 points)

1 Answer

0 votes

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?

by (16,806 points)

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

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

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.

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 animplayer = getnode("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")

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.