Changing the sprite of a child node

Godot Version

4.1.2

Question

Why does this code not work?

var led = get_parent().get_node("LED")
var new_texture = load("res://assets/LED-2.png")

func _ready():
	led.texture = new_texture

I have tried multiple methods such as using $ yet nothing happens. is there something wrong with my godot version or is it just the code?

Help us to help you.

  1. Post a screenshot of your scene tree, so we can see where stuff is and what it’s named.
  2. Post the errors or more context about what “It does not work” means.
1 Like

try change to

var new_texture = preload("res://assets/LED-2.png")

1 Like


here it is.

also I don’t get any errors it just doesn’t change the sprite.

doesn’t work :((

I don’t understand why you connect yourself to yourself via get_node, if your script already has itself, you can just delete it and refer to _ready via texture =… or self.texture. Do that, in the meantime I’ll read the documentation for you.

1 Like

This is basically all your code that should work:

extends Sprite2D
self.texture = preload("res://assets/LED-2.png")

try deleting your code, pasting this one and running it.

1 Like

oh, i dont test code, sorry, try delete “self.”

extends Sprite2D

func _ready():
	self.texture = preload("res://assets/LED-2.png")

it worked. I just added _ready(). thanks a lot!

1 Like

Sorry for the error at first, I just write in C#. good luck.

1 Like

No problem. thanks.

1 Like