Label is allways null // Null instance // HELP

Godot Version

So uhmmm i actually tried like 10+ hours now and i cant find any erros or clues how to fix this. I wanted to change a Label with some data of a dictionary which is perfectly loaded, until line 18. In my Output it easily prints the .name but it wont change the name_label.text ? Any suggestions ?

extends Control

var girl_id: int

@onready var name_label = get_node("/root/GirlProfile/NameLabel")
@onready var relationship_status_label = $RelationshipStatusLabel
@onready var relationship_progress_bar = $RelationshipProgressBar
@onready var info_label = $InfoLabel


func set_girl_id(girl_id: int):
	print("Girl ID set to: ", girl_id)
	
func update_profile(girl_id: int):
	var girl_data = Global.get_girl_data(girl_id)
	print("Girl data", girl_data)
	print("Updating profile for girl ID:", girl_id)
	print(girl_data.name)
	
	if name_label == null:
		print("name_label is null")
	else:
		name_label.text = girl_data.name
		print("Name label updated:", name_label.text)

	if relationship_status_label == null:
		print("relationship_status_label is null")
	else:
		relationship_status_label.text = Global.relationship_levels[girl_data["relationship_status"]]
		print("Relationship status label updated:", relationship_status_label.text)

	if relationship_progress_bar == null:
		print("relationship_progress_bar is null")
	else:
		relationship_progress_bar.max_value = Global.get_next_level_threshold()
		relationship_progress_bar.value = girl_data["relationship_points"]
		print("Relationship progress bar updated")

	if info_label == null:
		print("info_label is null")
	else:
		info_label.text = girl_data["info"]
		print("Info label updated:", info_label.text)

Debugg Output:

Girl ID set to: 1
Girl data{ “name”: “Maya”, “relationship_status”: 0, “relationship_points”: 0, “info”: “Info über Mädchen 1” }
Updating profile for girl ID:1
Maya
name_label is null
relationship_status_label is null
relationship_progress_bar is null
info_label is null
Menu Check

Please put your code between triple backticks, like so:
```
Code
```

To make it readable

1 Like

Hi, where is the name label relative to the script the node is on? Is it a child of the current node? If so, then you don’t need to use the root path.

Yeah i know, i just tied if it would work with that, all labels are childs yes …

Is the update profile function being called before the variables are ready?

yes, right here is a other scene where the function is called

``
extends Control

func _on_maya_pressed(girl_id):
var profile_scene = preload(“res://Scenes/girl_profile.tscn”).instantiate()
profile_scene.set_girl_id(girl_id)
profile_scene.update_profile(girl_id)
print(“Menu Check”)
get_tree().change_scene_to_file(“res://Scenes/girl_profile.tscn”)

func _on_charb_pressed(girl_id):
var profile_scene = preload(“res://Scenes/girl_profile.tscn”).instantiate()
profile_scene.set_girl_id(girl_id)
profile_scene.update_profile(girl_id)
print(“Menu Check”)
get_tree().change_scene_to_file(“res://Scenes/girl_profile.tscn”)
``

Hi, when you use the change_scene_to_file function, you are NOT using the instance stored in the profile_scene variable. It becomes an orphan node. Also, since you never add the profile_scene instance to the tree, the @onready variables are never initialized because that only happens after the node is added to the tree.

There is not need to instantiate the girl profile scene if you are using the scenetree change scene function. Instead, you can use autoloads to pass information between scenes. Or you can not use the scenetree change scene function and instead add the profile scene to the tree yourself. Make sure to only call the update profile function after it is added to the scenetree, though.

Alright, so i added this code on my button

func _on_maya_pressed(girl_id):
	var profile_scene = preload("res://Scenes/girl_profile.tscn").instantiate()
	profile_scene.set_girl_id(girl_id)
	get_tree().root.add_child(profile_scene)
	get_tree().current_scene = profile_scene

but still its giving null for all labels in my debugger …

So the first way is working now !
For anyone in the future who wants to replicate this.

Global Script:

var selected_girl_id: int = -1
func get_girl_data(girl_id):
	var girls_data = {
	1: {"name": "Maya", "relationship_status": 0, "relationship_points": 0, "info": "Info über Mädchen 1"},
	2: {"name": "Charb", "relationship_status": 0, "relationship_points": 0, "info": "Info über Mädchen 2"}
	}
	return girls_data[girl_id]

Menu Script where you change the scene:

extends Control


func _on_maya_pressed():
	Global.selected_girl_id = 1
	get_tree().change_scene_to_file("res://Scenes/girl_profile.tscn")

Lastly the Profile script

extends Control

var girl_id: int

@onready var name_label = $NameLabel
@onready var relationship_status_label = $RelationshipStatusLabel
@onready var relationship_progress_bar = $RelationshipProgressBar
@onready var info_label = $InfoLabel

func _ready():
	girl_id = Global.selected_girl_id
	set_girl_id(girl_id)
	
func set_girl_id(girl_id: int):
	update_profile(girl_id)
	print("Girl ID set to: ", girl_id)
	
func update_profile(girl_id: int):
	var girl_data = Global.get_girl_data(girl_id)
	print("Girl data", girl_data)
	print("Updating profile for girl ID:", girl_id)
	
	if name_label == null:
		print("name_label is null")
	else:
		name_label.text = girl_data["name"]
		print("Name label updated:", name_label.text)

	if relationship_status_label == null:
		print("relationship_status_label is null")
	else:
		relationship_status_label.text = Global.relationship_levels[girl_data["relationship_status"]]
		print("Relationship status label updated:", relationship_status_label.text)

	if relationship_progress_bar == null:
		print("relationship_progress_bar is null")
	else:
		relationship_progress_bar.max_value = Global.get_next_level_threshold()
		relationship_progress_bar.value = girl_data["relationship_points"]
		print("Relationship progress bar updated")

	if info_label == null:
		print("info_label is null")
	else:
		info_label.text = girl_data["info"]
		print("Info label updated:", info_label.text)

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