Class variable assigned null once instantiated

(Sorry about the bad topic tagging but nothing relevant was available? No OO, Class, etc.)

I have a class of Main_Player which Inherits from a class named Character, the Character class has an array of MonoBehaviour where scripts from an object are assigned. In the Main_Player constructor, a section of code runs through the Components on an object and adds the scripts to the array.

MonoBehaviour[] scripts = new MonoBehaviour[instance.GetComponents<MonoBehaviour>().Length];
for(int i = 0; i < instance.GetComponents<MonoBehaviour>().Length; i++)
{
    scripts _= instance.GetComponents<MonoBehaviour>()*;*_

}
When I attempt to print the state of ‘scripts’ under this same piece of code it produces the expected result of a generic ‘UnityEngine.MonoBehaviour[]’, however after the instatiation is complete and ‘scripts’ is accessed later on it comes back with null.
Is there anything obvious I am missing? I will provide more code if neccessary but don’t want to throw a mess of code on so I will await a initial response first to see if there is any general consensus on what I could be doing wrong.
The line of code that declares the ‘scripts’ variable:
public MonoBehaviour[] scripts;
Thanks very much in advance for anyone who spends time helping me out here! Very much appreciated.

You have actually shown us two lines that declare a scripts variable.

If it’s a member of the class, as suggested by the second code fragment (and the question title), then you don’t want to be declaring it again in the first fragment.

The first fragment declares a scripts variable that’s local to the function it’s in. That why things look ok if you log stuff within that function. But you’re not changing the member variable which is why that’s still null when you try to access it from elsewhere.

So, just change the first line of the first fragment to…

scripts = new MonoBehaviour[instance.GetComponents<MonoBehaviour>().Length];

…so that it sets the member variable rather than creating a local one.

By the way, GetComponents returns an array of the sort you want. So you’re reinventing the wheel with the first fragment. You could replace the whole of it with

scripts = instance.GetComponents<MonoBehaviour>();

(a lot more efficient since you’re calling GetComponents (n*2)+1 times where you could call it just once).