I try to load texture to a sprite in code using
$move_sprite.texture = load("res://sprites/defend_button.png")
But it gives me error:
get_node: (Node not found: "move_sprite" (relative to "/root/testscene/character/Camera2D/move_sprite").)
Invalid set index 'texture' (on base: 'null instance') with value of type 'StreamTexture'.
Judging by the errors I incorrectly inserted nodes name but I'm sure it's correct. Any ideas?

  • From the path in the error message I'd say the script is attached to move_sprite and wants to change its own texture. From the perspective of the script, the relative path to self it's not your own name but rather the keyword self. So instead of:

    $move_sprite.texture = load("res://sprites/defend_button.png")

    It should be:

    self.texture = load("res://sprites/defend_button.png")

    Since self is contextually implicit, this can be further shortened to:

    texture = load("res://sprites/defend_button.png")

The node this script is on does not have a child named move_sprite. Are you sure you have the right name, and are you sure it's a direct child of this node? Have you perhaps autoloaded this script, which means you have an autoloaded node that might be trying to run this code?

Either the node this script is attached to does not have a child named move_sprite or the code is executed before _ready. Maybe show your node tree?

From the path in the error message I'd say the script is attached to move_sprite and wants to change its own texture. From the perspective of the script, the relative path to self it's not your own name but rather the keyword self. So instead of:

$move_sprite.texture = load("res://sprites/defend_button.png")

It should be:

self.texture = load("res://sprites/defend_button.png")

Since self is contextually implicit, this can be further shortened to:

texture = load("res://sprites/defend_button.png")

    xyz Yes, thanks, I wanted to change it's own texture but I guess $ symbol works only for child nodes

    • xyz replied to this.

      TraitPhoe Yes, it's a relative path going "down" the children. The first name after $ should refer to an immediate child of the script's node.