Trying to find node returns null

Godot Version

<4.3 →

Question

I have a bug I reproduced it
heres a picture

heres the code
extends Control
var score = 0
@onready var label: Label = $Label

#or get node they both dont work

Called when the node enters the scene tree for the first time.

func _ready() → void:

print(get_children())
pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
label.text = ‘11’
heres the error:
Invalid assignment of property or key ‘text’ with value of type ‘String’ on a base object of type ‘null instance’.
its not a code error but something in the editor

Hello again, @sigmamaul9! I finally understand why you have error. You write a change of text property for label at _process function. At first frame the label is null (_ready still not called). Try to update the label by signals for more clear code.

I updated this part
func _process(delta: float) → void:
label = $Label
label.text = ‘11’
still gives same error
the script can find child node using get_children() function but cant directly get it
do you any settings or things in the inspector that can affect this.

So, it’s bad way… :laughing:

func _process(delta: float):
     label = $Label

You at each frame assign a $Label node to label variable.
To get the solution you don’t need to use _process function for change label.

Try this way:

extends Control


var score: int = 0
@onready var score_label: Label = $Label

signal score_count_changed

# Connect this function with score_count_changed signal
func on_score_count_changed():
     score_label.text = "Score: " + str(score)

# Call this when player reach the score
func add_score():
     score += 1
     score_count_changed.emit()

# For example
func _ready():
    # Call add_score function when Node is ready
    add_score()

yeah but trying to use the label does not work because label var is null
i think i could use object id but idk how

I really don’t understand why your label is null
You tried my example code from previous post?

yes it worked
accidentally added a letter it works its a miracle thank you
i think its a bug I will use this soultion as a workaround

No problem, have a nice day. :slightly_smiling_face:

@sigmamaul9 So, if you gets the solution, please, mark a post with solution for another peoples who are looking for a solution. Thank you!

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