0 votes

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.

Godot version Godot standart 64x
in Engine by (18 points)

2 Answers

+1 vote

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.

by (2,628 points)

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.

0 votes

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.

by (18 points)
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 Frequently asked questions and 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 [email protected] with your username.