Strange nullreference exception?

Heya,

It is about the panel_load_menu.
All three panels are children of a Canvas.

During the For…Next it is found and assigned.
It is also set to SetActive(false) without any problem and I can with a button set it active and false.
But the compiler keeps telling me I have a nullreference error to an object that is not null?

I can compile the code, start playmode and even Build and Run and turn the panel On and Off,
yet even in debug mode of the build I get the error message

Why?

GameObject[] child = new TargetChildObjects("canvas_main_menu(Clone)").targetchildobjectsArray;
        for(int i = 0; i < child.Length; i++)
        {
            if (child[i].name == "panel_main_menu") { panel_main_menu = child[i]; }
            if (child[i].name == "panel_load_menu") { panel_load_menu = child[i]; }
            if (child[i].name == "panel_newplayer_menu") { panel_newplayer_menu = child[i]; }
        }
            panel_load_menu.gameObject.SetActive(false); //<= Error points to this line
            panel_newplayer_menu.gameObject.SetActive(false);// <= Why not to this one.

ps: my first code was just 'panel_load_menu.SetActive(false) ’ but it gave me the same error

Compilers don’t

I believe that’ll be the runtime not the compiler. But who says the unstated object is “not null”, you? I’m siding with the runtime :wink:

Anyway you didn’t give the full error but going to gamble and say that it’s “new TargetChildObjects(“canvas_main_menu(Clone)”).targetchildobjectsArray;” that at least once returns null early in your game’s execution. Likely this code is being called before it has been initialised?

@nat42
Ah ‘Terminology’. “So my runtime kept telling me.” (sounds strange though to say my runtime. But yes ‘runtime error’ I know Xd)
But that aside…

But Found the solution.
Had to do with nested levels of children in the prefab and was looking for children of…
I just skipped it and am looking for ALL gameobjects,
But at least it finds it and assigns it.

Thanks tho