Referencing a Resource to Change a Sprite Texture

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

I think this question is pretty basic and I’m missing something simple… but I’m just starting out

My hierarchy is this…

A Scene for a specific Character

  • Node2D named “Character”
    • Sprite2D “CharacterSprite”

At the parent node I have a script with the following…

extends Node2D
class_name Character
@export var CharacterSheet : Resource
# Called when the node enters the scene tree for the first time.
func _ready():
	$CharacterSprite.texture = CharacterSheet.Visual

My intentions are for every Scene where this Character Node is added, attach a Resource to the variable CharacterSheet which itself has the variable Visual. Here’s that resource:

extends Resource
class_name CharacterSheet
@export var Visual : Texture2D

Even after I link the Resource to the Character Node, I’ve got nothing shown when ran. If I manually put the .png in to the Sprite2D and remove my assignment it appears, so I’m assuming I’m close… Do I have my hierarchy/ordering wrong somewhere?

:bust_in_silhouette: Reply From: bamboo_river_kid

I believe you’re missing:

var picture = load(“res://name of your picture.png”)

Then change in the ready() function:
$CharacterSprite.texture = picture

That was it. Thank you!!

StankWilliams | 2023-04-22 18:21