How to change a sprite texture with an array?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MaximoTG98

Hello everyone!

So,i’ve been struggling to change the sprites texture.What i’m trying to achieve is change the sprite image of a node using a resource path(e.g.“res://Sprites/crop0.png”) that is inside of an array.

The array has 4 resources paths all of them are the same as the example.
This is what i wrote: sprite.texture = crop[1]
But if i try to run the game i get an error:Invalid set index 'texture' (on base: 'Sprite') with value of type 'String'.

I also tried to change the texture_normal of a texturebutton and it doesn’t work as well.Same error.I didn’t make any typo at the path…

And that was it… Let me know if you know how to fix this or even an idea please.
Have a good day.

:bust_in_silhouette: Reply From: Parjina

Hi I am a beginner in godot, too. But I guess the problem is you are trying to put a ‘String’ to that sprite’s texture. String is not a texture as error mentioned (" with value of type ‘String’. ") :slight_smile: you should ‘load’ that texture :slight_smile:

You can do it like that: sprite.texture = load(crop[1])

:bust_in_silhouette: Reply From: luckyNyaNya

sprite.texture expects a Texture
crop[1] gives you a String
what you need to do is to load that resource:
sprite.texture = load(crop[1])

:bust_in_silhouette: Reply From: Pin

Your array crop[1] is String not Texture
All you need is load()
sprite.texture = load(crop[1])

You can use AnimatedSprite Node to get the same result You can watch the tutorial from Here