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…