Array only initialized when serializable[SOLVED]

Hi, i’m getting some NullReferences erros because my array only initiated when i serialize my class and i did this test on blanks scripts with basic code like

EXAMPLE CODE

[System.Serializable]
public class SomeClass
{

}

[SerializeField] private SomeClass[ ] testArray;

private void Awake()
{
testArray = new SomeClass[10];
}

if i do that works perfectly as expected but when i remove the System.Serializable or the SerializeField or even when i hide my SomeClass from inspector my array don’t initiate BUT if i put the debug to show the testArray
suddenly my array got initiated, this is a Unity bug since i can do this code on compilers and i don’t get any NullReference, so what should i do? just let some class on the inspector since unity have this problem? and again, i already tried this exactly same code on compilers and they instantiate my classes perfectly and this problem don’t happens if i do simple arrays like int[ ] intArray = new int[10] and i was not used to get thoose problems in later versions of Unity, so something changed or my unity is bugged?,

Currently using unity LTS 2019.

Cheers.

EDIT: The fixs codes on the internet made my perfomance drop from 1.200fps to 15fps so i’ll let serialized even without necessity since there’s no other way(Until now).

EDIT02: Fixed restarting Unity and compiling the code again(don’t know how)

Please use code tags .

If you use Unity’s inspector to look at a variable containing a serializable class, and the object is null, Unity will replace it with a new (non-null) instance instead. This only happens in the editor when you are actually looking at that object and is a side-effect of the fact that the JSON serializer Unity uses for this doesn’t allow nulls.

If you were also using the inspector to set the initial value of your variable, then this probably wouldn’t matter. But since you are overwriting the default values in Awake (creating a new array to replace the serialized one) you get all the normal C# rules for data, which means your array starts out filled with null.

If you don’t want nulls, you can easily add a loop in your Awake function to assign new objects to each slot in the array.

If the problem is that you DO want nulls, and Unity is replacing them, then you’re just going to have to stop letting Unity serialize the array, sorry. That means it won’t be visible in the inspector, so if you want to see the data at runtime you’ll need to figure out another way to do that.