- Home /
The question is answered, right answer was accepted
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!!!
Answer by bobisgod234 · Aug 10, 2017 at 12:20 AM
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.
Actually I Forgot To Put [System.Serializable] In The Question, But Still It Doesn't Work And Gives: NullReferenceException!!! It Can Work When You Recompile Or Edit The Script! Or Change (Index) Value! This Seems Like A Bug To $$anonymous$$e In ExecuteInEdit$$anonymous$$ode().
The default value you are assigning to myType is null. I am not sure why you are assigning null to a variable you want to access. You cannot access any properties of a null variable, and that is why you are getting NullReferenceExceptions. This is certainly not a bug with ExecuteInEdit$$anonymous$$ode.
I presume the reason your original code works only when you recompile is that recompiling forces Unity to serialize your array, and since Unity's serializer can't handle null values, it uses a 0 length array ins$$anonymous$$d.
To assign a default value to it when the script is assigned, change
public $$anonymous$$yClass[] myType = null;
To
public $$anonymous$$yClass[] myType = new $$anonymous$$yClass[0];
Actually I Didn't $$anonymous$$now How To Re-New The Array Classes In JavaScript, But "= new $$anonymous$$yClasses[0];" Seems To Work, But I Think I Tested That And Didn't Work, But For Some Reason It Seems To Work :D
Thanks..
Answer by unidad2pete · Aug 09, 2017 at 06:21 PM
If you set null to myType class, all references recives null ;)
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[] { };
}
}
well, This Doesn't Work In JavaScript, It Gives $$anonymous$$e Errors About This Line:
new $$anonymous$$yClass[] {};
Oh, im sorry, i dont know how make new class on Java :( its c# code and run fine on my PC
No Problem, I Try To Figure It Out :)
[EDIT] Actually I $$anonymous$$now How To Create Non Array Classes!!
$$anonymous$$yClass = new $$anonymous$$yClass();
But I Don't $$anonymous$$now How To Create Array Classes!
Follow this Question
Related Questions
Need a hand using array.Length in a C# editor script 1 Answer
NullReferenceException error in an array of objects 0 Answers
Toggle ON/OFF MaterialChange 0 Answers
Array of custom properties? (C#) 1 Answer