Help why is my label >null<?

4.4

This is my first post so i dont know everthying!
So i made a score for my clicker game and i made a shop too and i want the shop to check if your score is the same as the price or higher so you could buy it, if its lower than the price then you cant buy it, i made my score script global so that i could access the score in the shop script

Hi,

To refer to the label in the scene directly use:

$Label.text = "blah"

Name is also case sensitive. Or CTRL / CMD drag the label into the code to create a reference name in your code.

Like the following

@onready var label: Label = $Label

Also best to give your controls descriptive names that don’t match the control type name.

Kindly

Ryn

1 Like

But i’m making a score and i dont want it to say something specific, i want the numbers to increase, i have already made the code for the score to increase, and i have already made an @onready variable, and when i made my script global it started saying my label is null and my game doesnt work anymore, when i remove it from autoloads it becomes normal and works

Hi,

I only used “Blah” as an example. You can still use score as you have done:

$Label.text = "Cash: " + str(score)

Without seeing the entire code it’s hard to be specific. When you make something global you need to prefix the variable with the global script name.

EG:

$Label.text = "Cash: " + str(globalsScriptName.score)

Kindly

Ryn

Wow thanks! It works, i removed my score from globals and made a different script global and make it have the same variables, and switched “score” to “Global.score”

1 Like

Hi!
Yep : maybe you can show us the top of your script. The reference might be missing.
And even if it worked, for now, your score would print 0.
You need to access the global variable directly as Ryn showed you.

In your _ready() you can write this (replace with your own names) :
score = GlobalScript.score

And in your on_touch_screen_button_pressed(), you must do the same.
The variable “score” here seems to not be useful as you pointed out that your score is computed in a Global script.

But, share more of your code, it’ll help!

Cheers!

1 Like

Dont worry! Its already fixed! I appreciate you for trying to help Marraw! Thanks. i already fixed everyrhing already, and im experienced with godot so yeah um Cheers!

2 Likes