0 votes

I have two scripts, first is AsteroidSpawner script that literally spawns object named Asteroid, and second is HUD that control all UI related stuffs.

There are some labels in UI, so I used onready to get those nodes:

extends CanvasLayer

class_name HUD

onready var asteroids_label = $Control/RightContainer/AsteroidCountLabel

func set_asteroids_label(value: float):
    asteroids_label.text = "Asteroids: " + str(value)

However, when I try to access HUD script, with setasteroidlabel, it gives me null error:

var spawned_asteroids: Array = []
onready var hud: HUD = get_tree().get_root().find_node("HUD", true, false)

func _ready():

func spawn_asteroids():
    ... spawn asteroid ...


    hud.set_asteroids_label(spawned_asteroids.size()) # UPDATE HUD

Invalid set index 'text' (on base: 'Nil') with value of type 'String'

I guess that HUD is not ready, but AsteroidSpawner script try to accessing it, wasn't it? Then how should I wait until HUD is loaded?

in Engine by (135 points)

1 Answer

+4 votes
Best answer

yield(ObjectReference, "ready")
print("Dinosaurs: 0_0")

by (398 points)
selected by

Awesome. Works good!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.