Puzzling Class Constructor Problem

The following two classes represent a pattern I’ve organized. The basics of it are that one class (let’s call it MyClass) has a parameterless constructor that adds the instance of that class to a static MyClass array (which for purposes of simplification I have here included within MyClass as well). The second class is a MonoBehaviour that has member fields of MyClass, and they are initialized in their declaration with the custom default constructor. The problem I have is that when I have, let’s say, 3 member MyClass fields, the length of the static MyClass array ends up equal to 6. No matter how many times I instantiate a MyClass, the array ends up twice as long.

HOWEVER, if I initialize the MyClass members with the same default constructor in a method body, such as Awake(), etc., the array ends up with the appropriate length. Now, it might seem like an obvious solution is to wait and initialize after the declaration, but the code is intended to be as user friendly as possible, so I really want the constructor to take care of everything. There seems to be no reason that this can’t be done, so I’m hoping someone can give some insight into the problem.

Here’s the code

public class MyClass
{
	public static MyClass[] staticArray;
	
	public void AddToStaticArray()
	{
		if (staticArray == null)
		staticArray = new MyClass[] {this} ;
		
		else
		{
			MyClass[] newArray = new MyClass[staticArray.Length + 1];
			for (int i = 0; i < staticArray.Length; i++)
			{
				newArray _= staticArray*;*_

* }*
* newArray[staticArray.Length] = this;*
* staticArray = newArray;*
* }*
* }*

* public MyClass () {AddToStaticArray ();}*
}

//Scenario 1
public class MyMonoBehaviour : Monobehaviour
{
* MyClass class1 = new MyClass();*
* MyClass class2 = new MyClass();*

* void Awake() {print (MyClass.staticArray.Length);} //output is 4*
}

//Scenario 2
public class AltMyMonoBehaviour : Monobehaviour
{
* MyClass class1;*
* MyClass class2;*

* void Awake()*
* {*
* class1 = new MyClass();*
* class2 = new MyClass();*
* print (MyClass.staticArray.Length);} //output is 2*
* }*
}

I figured out the problem, and it has to do with the way that the serializer works with default constructors. I was able to make the array have the correct length in two ways: 1) by marking each field with a NonSerializableAttribute (which was not ideal because at the very least I want users to be able to modify the class from the Inspector), and 2) by not having a default constructor with the AddToStaticArray() method. Even when I wasn’t using the default constructor to initialize my fields, the serializer was calling it multiple times, apparently. So, since I wanted to keep serialization, I decided to forego the default constructor.