Invalid get index 'color' (on base: 'null instance').

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By R0dL0d

welp, idk whats going on. My game don’t run becuse of this erro “Invalid get index ‘color’ (on base: ‘null instance’).” somthing with color related, but i really don’t know what to do. i’m doing like a candy crush game for a school project.
the code that is on error is this:

func spawn_pieces():
for i in width:
	for j in height:
		if !restricted_movement(Vector2(i, j)):
		#choose a random number and store it
			var rand = floor(rand_range(0, possible_pieces.size()));
			var piece = possible_pieces[rand].instance();
			var loops = 0
                            while(match_at(i, j, piece.color) && loops < 100):
				rand = floor(rand_range(0, possible_pieces.size()));
				loops += 1;
				piece = possible_pieces[rand].instance();

the error is in:

 while(match_at(i, j, piece.color) && loops < 100):

if somone know what’s going on i will be thankfull.

:bust_in_silhouette: Reply From: Lopy

The error message means that you are trying to read the variable “color” inside “piece”, but “piece” does have this variable, as it is equal to null, which has no variable. null is a special value often used to represent a corrupt Object, and is returned for instance when creating an Object failed. My guess is that possible_pieces[rand].instance(); failed for some reason, returning a null into piece. You can try printing the content of piece (and rand), to see what happens exactly.


PS: You have unnecessary ;s at the end of your lines. You can leave them if you like the aesthetic, but I want to make sure you know they are optional.

Now that you say it, really this code possible_pieces[rand].insetence(); is not working
I have no idea why this happens. any suggestion?
i tried to print the variable but I think it didn’t work.

PS: i kinda new to godot so i don’t know so much stuff. i better stay the ;, Plus i think is cool.

R0dL0d | 2022-09-19 23:50

:bust_in_silhouette: Reply From: cymrix

Not sure if this is relative, but this is one of the top results when searching “godot invalid get index on base null instance”.

After much trial and error, I found that the child nodes of my root node are not accessible until the “ready” function. If I tried to reference them at the top of the script or in the “init” function, I just kept getting “null”.

Maybe this is common knowledge for experienced godot people, or it’s listed somewhere I haven’t seen yet, but it really made my life difficult for two days.