Label is not displaying text in game

Godot Version

4.2.2

Question

This used to work but I changed from using a signal to just using instantiating for testing purposes. The text of the label does not display in game. The player picks up a card and the text on screen should say 1/1 cards.

I have a label with a script attached that only displays text and takes in two parameters. The text appears in the output thanks to the GD.Print() function.

public partial class AmountLabel: Label
{
public void DisplayScore(int amount, string name)
{
Text = $“{amount}/1 {name}”;
GD.Print(Text);
}
}

The parameters come from the card.cs script.

public partial class Card : Node
{
public int cardAmount = 0;

public void OnCardCollected()
{
    cardAmount++;
    AmountLabel label = new();
    label.DisplayScore(cardAmount, "Card");
}

}

This function is called from the collectable.cs script.

public partial class Collectable : Node
{
public void Collect()
{
Card card = new();
card.OnCardCollected();
QueueFree();
}
}

This collectable script is called from the player.cs script

public void TouchCollectable()
{
for (int i = 0; i < GetSlideCollisionCount(); i++)
{
KinematicCollision3D collision = GetSlideCollision(i);

        if (collision.GetCollider() is Collectable collectable)
        {
            collectable.Collect();
            break;
        }
    }
}

There is text in the Text variable of the label, since it is being displayed in the output, but is not being displayed on screen. The label scene is the normal scene with only the font colour changed.

So, why is the text not displaying in-game?

check how node looks on remote
obraz
when game is running.

Is the dollar sign in $"{amount}/1 {name}" intentional? Never seen that as a C# feature but in GDScript that attempts to retrieve a node path.

Yes, it is. It allows you to have variables directly in a string.

EDIT:

	public MyTest() {
		int v_int = 100;
		string v_string = "Hello, World!";
		float v_float = .001f;
		GD.Print ( $"int:{v_int} string:{v_string} float: {v_float} this:{this}");
	}

program output:
int:100 string:Hello, World! float: 0.001 this:<Node2D#27900511495>

the source code below works. press A to change the string.

AmountLabel label = new();

maybe i am missing something but i don’t see where you assign AmountLabel a parent.

using Godot;
using System;
public partial class MyLabel:Label {
	public void TextSet(string a_text) {
		Text = a_text;
	}
}
public partial class MyTest:Node2D {
	MyLabel m_my_label = null;
	public override void _Ready() {
		m_my_label = new MyLabel();
		m_my_label.TextSet("Hello, World!");
		AddChild(m_my_label);
	}
	public override void _Input(InputEvent a_event) {
		if(a_event is InputEventKey _a_event_key) {
			if(true == _a_event_key.Pressed && Key.A == _a_event_key.Keycode) {
				m_my_label.TextSet("The A key was pressed!");
			}
		}
	}
}

is was added in c# 10

I attempted using this:

public partial class AmountLabel: Label
{
public void TextSet(string aText)
{
Text = aText;
}

public void DisplayScore(int amount, string name)
{
    AmountLabel label = new();
    label.TextSet($"{amount}/1 {name}");//= //Text = $"{amount}/1 {name}";
    AddChild(label);
    GD.Print(Text);
}

}

and it still is not working. Bear in mind that the AmountLabel script is attached to the label. What do you mean about assigning AmountLabel a parent, as in a parent node?

is already label don’t needed new copy inside.
I will ask again. Did you check remote? maybe is working but you don’t see?

1 Like

I’m not sure what remote is supposed to do. The amountLabel label is there when i click the remote button while the game is running. Writing text into the text box adds it to the screen, but when I collect the object the text does not change still.

You use remote to check variables… we don’t see your project
This code is works:

using Godot;

namespace LabelTest;
public partial class LabelTest : Node
{
	Label Label { get; set; }
	public override void _Ready()
	{
		Label = new();
		AddChild(Label);
	}

    public void UpdateLabelScore(int score, string name) => Label.Text = $"{score}/1 {name}";
}

tested with Button
obraz

obraz

obraz