Sprite won't show when added through code

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

This is my code:

var tag = Sprite.new()
tag.name = "Test"
tag.texture = load("res://images/test.png")

The sprite won’t show up and idk why. This is probably very stupid and I am missing something very obvious.

:bust_in_silhouette: Reply From: mrfatalo

I made it work by calling the texture with onready var and
preload. (At the top of the script as rule for works)

 onready var texture := load("res://images/test.png") as Texture  

Safe Cast := and as Texture

Now your code (which I put in a standard _ready() function):

func _ready() -> void:
        var tag := Sprite.new() 
    	tag.name = "Test"
  • Safe casting wiith :=
  • Removed "." because already in root
  • Used set_texture for set the onready preload texture

I don’t know if it’s the answer you were looking for, I hope it can help you, Bye!