So, I'm making a loading screen, and I've written this code:

extends Control

#GUI
@onready var color_rect = $ColorRect
@onready var label = $Label

func _ready():
	var colors = [Color.BLACK,Color.AQUA,Color.CORNFLOWER_BLUE,Color.CHARTREUSE,Color.DARK_TURQUOISE,Color.CRIMSON,Color.DARK_RED,Color.DEEP_PINK,Color.ORANGE,Color.ORANGE_RED,Color.GOLD,Color.WHITE,Color.TOMATO,Color.TEAL]

	color_rect.color = colors.pick_random()
	if color_rect.color == Color.WHITE or color_rect.color == Color.GOLD:
		label.modulate = Color.BLACK

This is the hierarchy of the scene, the previous script is attached to the root:

When I launch the scene I get this error: "Invalid set index 'color' (on base: 'null instance') with value of type 'Color'.", as if the ColorRect did not exist... why?? I did not rename it, and it is in the hierarchy, why is it not detecting it?

  • xyz replied to this.

    xyz

    That sent me down a rabbit hole...
    So, this scene I'm working on is intended to be a loading screen, which pops up after clicking "Play" in the main menu. The main menu is another scene, the MAIN scene, the one that appears when I launch the game. Well, according to the remote (I didn t even know that thing existed, but it's super useful, thanks!) the script that is throwing the error is loaded, but the scene and the hierarchy it is attached to, aren't. Why? Because I set the script to be autoload, and I guess it cannot access a scene that isn't instanced, or in the hierarchy, because they do not exist at that time...

    Again, thanks! I'll try and see how to access the nodes from the loading script, I guess I'll have to search for them with get_node().

    Edit: I was following this tutorial and they address something similar at the end