How can I change the animation frame for an instance? (Invalid set index 'frame' (on base: 'null instance') with value of type 'int'.)

Godot Version



I want to change the frame of instance such that any newly created ones have a difference frame. the issue is in the line $coffeeliquid.frame = Global.Currentdrink.

I keep getting this error:
Invalid set index ‘frame’ (on base: ‘null instance’) with value of type ‘int’.
I’ve been stuck on this problem for a while now. what can I do?

The node you have as the root node of the scene is the same one the script is on?

$Drinkjug points to a Node that does not exist, thus becoming null at runtime, giving you that error.
Either add the Node named Drinkjug or remove that line.

the part that’s not working is : $coffeeliquid.frame = Global.Currentdrink
Drinkjug is in another scene and it doesn’t give any errors.

So there is no node that has both coffeeliquid and Drinkjug as direct children?

I’m sorry but I don’t understand. I thinks it does?

yes, there is no node that has both coffeeliquid and Drinkjug as direct children .they are in two different scenes

The $ syntax is just shorthand for get_node() and thus expects the Node to have the name after the $ symbol. If that node doesn’t exist in the current node tree the script is attached to, it becomes null.

so how can I change it? do I put Drinkjug and Coffeeliquid as direct children under a node

It depends on what you intend to do. I can’t make a guess, you gotta tell me.
You can check for null with if variable == null

I want switch the animation frame of instances of Coffeeliquid based on the current value of a global variable. The drinkjug part isn’t very important, but even when I delete it, it still gives me the (Invalid set index ‘frame’ (on base: ‘null instance’) with value of type ‘int’.) error. I cant seem to animate an instance at all

Huh, post the code as it is now, im not sure what you mean, but I have ideas.

I commented out the drinkjug part

And what happens when you click on the error you get?

Nothing happens?

do you mean this error?

Show the node tree that contains a node named “cup”

@Efi thank you so much btw. I’ve just started godot, and everything seems hopeless

and that scene is an instance of “coffee.tscn”? If not, did you mean to have a different script on it?

Everything seems hopeless because of capitalism. That is mostly unrelated to Godot.

cup is a scene for where the drinkjug node is. it has the same script as the coffeeliquid node in coffee.tscn.