Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!!

I’m Very Disappointed In Using ExecuteInEditMode(), Whenever I’m Dealing With Arrays And Lengths, The ExecuteInEditMode() Will Make Everything Worse!!
When Checking For Array Length: NullReferenceException
When Clamping An Index Value Between 0 And Array.Length: NullReferenceException
When Clicking Something: NullReferenceException
Everything: NullReferenceException!!
However, The Error Goes Away When I Edit Or Recompile My Script!!
Here Is An Example Code I Made, Which Is Very Very Simple, But NullReferenceException!!!
-C#:

using UnityEngine;
[ExecuteInEditMode()]
public class TEST : MonoBehaviour
{
	public int index = 0;
	public MyClass[] myType = null;
	[System.Serializable]
	public class MyClass
	{
		public float myVar = 0f;
		public int _myVar = 0;
		public Transform m_myVar = null;
	}
	public void Update ()
	{
		index = (int)Mathf.Clamp(index,0,Mathf.Clamp(myType.Length - 1,0,Mathf.Infinity));
	}
}

-JavaScript:

@script ExecuteInEditMode()
public var index : int = 0;
public var myType : MyClass[] = null;
public class MyClass
{
	public var myVar : float = 0;
	public var _myVar : int = 0;
	public var m_myVar : Transform = null;
}
public function Update ()
{
	index = Mathf.Clamp(index,0,Mathf.Clamp(myType.Length - 1,0,Mathf.Infinity));
}

Can Someone Help Me With This?? I’ve Been Suffering From This Error For A Long Time!!
Also The Error Points At Clamping Line Which Has myType.Length In It!
However In Some Scripts It Works Well, But In Newer Scripts, NullReferenceException!!!

If you set null to myType class, all references recives null :wink:

        public int index = 0;
        public MyClass[] myType;
        public class MyClass
        {
            public float myVar = 0f;
            public int _myVar = 0;
            public Transform m_myVar = null;
        }
        public void Update()
        {
            if (myType != null)
            {
                index = (int)Mathf.Clamp(index, 0, Mathf.Clamp(myType.Length - 1, 0, Mathf.Infinity));
            }
            else
            {
                myType = new MyClass[] { };
            }
        }

Unity is not serializing your class “MyClass”, and as such “myType” is being left as null.

myType.Length

You cannot access the length of a variable that is just null, it has no object assigned to it, and as such, no length. This is the piece of code that is causing the null reference exceptions.

You can make Unity serialize your class by adding the System.Serializable attribute to it:

 using UnityEngine;
 [ExecuteInEditMode()]
 public class TEST : MonoBehaviour
 {
     public int index = 0;
     public MyClass[] myType = null;

     [System.Serializable]
     public class MyClass
     {
         public float myVar = 0f;
         public int _myVar = 0;
         public Transform m_myVar = null;
     }
     public void Update ()
     {
         index = (int)Mathf.Clamp(index,0,Mathf.Clamp(myType.Length - 1,0,Mathf.Infinity));
     }
 }

Now, Unity will initialize and serialize the array for you, and as such, it won’t be null.