how to change sprite from code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Yamz
$Sprite.texture = "res://Img/black.png"

returns this error
Invalid set index ‘texture’ (on base: ‘Sprite’) with value of type ‘String’.

I thought this was very easy, but I have not found a way to do it

1 Like
:bust_in_silhouette: Reply From: jgodfrey

You need to wrap that image reference in a load call. So, this…

$Sprite.texture = load("res://Img/black.png")

Two questions

  1. if i had a bunch of sprites say 1-5 and had a rng pick one how would i load that one. If i had it set up like var 1 = sprite path ect and then did $Sprite.texture = load (1) would that work?

2)how do you find the correct path?

Thank For your help.

ForgottenStar | 2021-12-12 22:40

question 1:
in theory, yes it will.

question 2:
go to the picture in the godot editor, right click it and press copy path.
happy coding!

modev | 2022-01-25 19:31