Label text not showing up

Godot Version

4.2.2 stable

Question

The problem is that when I create a label (stored in a separate scene that I create) and change the text from the default I have set, the string “Nothing” to “H”, it works, but if I try and do it a second time the label has no text.

This is very vague, can you post a screen record of it?

what is the code that you use to change the text of the label?
You have stored the label in a separate scene? This means you have a reference to the label scene, right?
In that case labelscene.text = "Test", assuming your Label is the root node of your scene.
If you set the text in your _ready() method, then it is only called once.

It says new users cannot upload

you can upload on https://anonymfile.com/

var LABEL = load(“res://Prefabs/label.tscn”)
var label = LABEL.instantiate()
ROOTNODE.add_child(label)
label.text = newtext
label.set_z_index(100)

okay. Based on this, I know that your label variable is gone once the method ends.
Can I ask you to create a local label variable inside your script?


var label: Label

func wherever_this_is() -> void
    var LABEL = load(“res://Prefabs/label.tscn”)
    label = LABEL.instantiate()
    ROOTNODE.add_child(label)
    label.text = newtext
    label.set_z_index(100)

once this method ends, you still have a reference to your label. You can then do label.text = "XXX" outside of the method (as long as the label actually exists in the SceneTree).

I also assume that your label position is set correctly when you add the label child into your ROOTNODE, because you wrote that you can see the text changing the first time.

I recommend this method:

  1. Create all prefabs in one scene
  2. Load it:
var Prefabs = load(“res://Prefabs.tscn”) #Path for example
  1. Get child:
var Prefab_Label = Prefabs.get_child(0) #0 is example index
  1. Optimize and use label:
Prefab_Label.text = newtext
Prefab_Label.set_z_index(100)
ROOTNODE.add_child(Prefab_Label)

But I’m not sure it’s what you need.

Thanks this worked perfectly I just assigned the labels text in the process function

for the moment that would work fine but I would encourage you to create a system so the setting of the label text only works when you need it.
The _process() method runs once per frame so the text is repeatedly set which could lead to performance issues

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.