Strange Error in a code that only affects one level, despite being used in all others

Godot Version

Godot Engine v4.3.stable.official [77dcf97d8]

Question

I am making my first platformer game (I started Godot a few weeks ago), and am adding a stopwatch to each level to record the time, and am only having trouble on level 1, despite using the same script on all other levels.
My problem is that when I start running Level_1, I get ‘Invalid assignment of property or key ‘text’ with value of type ‘String’ on a base object of type ‘Nil’.’
I have correctly assigned the stopwatch label to the stopwatch variable I made, and stopwatch to the global group: stopwatch, so I am unsure why this is happening.
I’ve attached a screenshot of the scene tree, attach the game_manager.gd script, and the stopwatch.gd script.
Scene tree:


This is the game_manager script;

extends Node

var points = 0
var lives = 3
var kills = 0
var stopwatch = 0


@onready var label: Label = %Label
@export var hearts: Array[Node]
@export var current_level : PackedScene
@export var end_level : PackedScene
@export var stopwatch_label : Label

func _ready():
	stopwatch = get_tree().get_first_node_in_group("stopwatch")
	label.text = "Points: " + str(points)
	
func _process(delta):
	_update_stopwatch_label()
	
func _update_stopwatch_label():
		stopwatch_label.text = stopwatch.time_to_string()
		GlobalVariables.global_time = stopwatch.time_to_string()
	
func decrease_health():
	lives = lives - 1 
	print(lives)
	for h in 3:
		if (h < lives):
			hearts[h].show()
		else:
			hearts[h].hide()
	if (lives == 0):
		get_tree().reload_current_scene()
	GlobalVariables.global_hearts = lives
		
func increase_health():
	lives = lives + 1
	print(lives)
	for h in 3:
		if (lives < h):
			hearts[h].show()
		else:
			hearts[h].hide()
	GlobalVariables.global_hearts = lives

func add_point():
	points += 1
	print(points)
	label.text = "Points: " + str(points)
	GlobalVariables.global_points = points

func add_kill():
	kills += 1
	print("kills:" + str(kills))
	GlobalVariables.global_kills = kills

and this is the stopwatch script:

extends Node
class_name Stopwatch

var time = 0.0
var stopped = false

func _process(delta):
	if stopped:
		return
	else:
		time = time + delta
		
func _reset():
	time = 0.0
	
func time_to_string():
	var msec = fmod(time, 1) * 1000
	var sec = fmod(time, 60)
	var min = time/60
	#formatting to look 00:00:000
	var format_string = "%02d : %02d : %02d"
	var actual_string = format_string % [min, sec, msec]
	return actual_string
	GlobalVariables.global_time = actual_string`

If anyone can help me, thank you, because I have been stuck on this bug for several days, and cannot get around it…

The first test is if the node you are after is really part of the group.

func _ready():
	stopwatch = get_tree().get_first_node_in_group("stopwatch")
    print(stopwatch)

If this prints null then get_tree().get_first_node_in_group("stopwatch") is failing. Check the spelling and case of your group name.
(I am also curious about the stopwatch group. Is there going to be multiple stopwatches?)

And whats this all about?
var stopwatch = 0
stopwatch is going to be a stopwatch object so why are you initializing it as an int?
If you give your variables types you will save preempt a lot of difficult to find bugs.
var stopwatch:Stopwatch

1 Like

@sancho2
There will only be one stopwatch, and I’ve made the changes you suggested (the ‘stopwatch’ variable is not null as it show a number when I hover my mouse over it, and nothing is printing), but the debugger is pointing the problem towards this:

func _update_stopwatch_label():
		stopwatch_label.text = stopwatch.time_to_string()
		GlobalVariables.global_time = stopwatch.time_to_string()

as apparently stopwatch_label is null, even though I have assigned the stopwatch label to it in the inspector.
I also changed the group name to ‘Stopwatch_1’ to make sure it wasn’t a case error.

Thanks