- Home /
The question is answered, right answer was accepted
Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors??
Now I'm Facing Another Problem With Array.Length In ExecuteInEditMode().
I'm Trying To Make An Automatic Button To Set And Create Default Recommended Values, So When I Try To Change A Value From The Array(Class), It Gives Me NullReferenceException!
Here Is An Example Script I Made Which Doesn't Work: -C#:
using UnityEngine;
[ExecuteInEditMode()]
public class TEST : MonoBehaviour
{
public bool create = false;
public MyClass[] myType = new MyClass[0];
[System.Serializable]
public class MyClass
{
public float myVar = 0;
public int _myVar = 0;
public Transform m_myVar = null;
}
public void Update ()
{
if(create)
{
if(myType.Length == 0)myType = new MyClass[1];
if(myType[0].myVar == 0)myType[0].myVar = 1;
if(myType[0]._myVar == 0)myType[0]._myVar = 2;
if(myType[0].m_myVar == null)myType[0].m_myVar = transform;
create = false;
}
}
}
-JavaScript:
@script ExecuteInEditMode()
public var create : boolean = false;
public var myType : MyClass[] = new MyClass[0];
public class MyClass
{
public var myVar : float = 0;
public var _myVar : int = 0;
public var m_myVar : Transform = null;
}
public function Update ()
{
if(create)
{
if(myType.Length == 0)myType = new MyClass[1];
if(myType[0].myVar == 0)myType[0].myVar = 1;
if(myType[0]._myVar == 0)myType[0]._myVar = 2;
if(myType[0].m_myVar == null)myType[0].m_myVar = transform;
create = false;
}
}
When I Edit The Script Or Recompile The Script It Will Work!!
Even If I Check The Length Or The Array It Self, It Doesn't Work:
if(myType) Or if(MyType != null) Or if(myType.Length > 0) Or if(myType.Length != 0), None Of Them Work!
But In This Way It Works Fine, But I Have To Click The Button 2 Times To Make It Work:
if(myType[0]) Or if(myType[0] != null).
Thanks...
Answer by bobisgod234 · Aug 10, 2017 at 07:22 AM
if(myType.Length == 0)myType = new MyClass[1];
You create a new array of MyClass that is 1 long. Without assigning any instances of MyClass to that array, it will just be filled will null.
if(myType[0].myVar == 0)myType[0].myVar = 1;
Here, you try to access the first element of myType, which is still null as you never assigned anything to it. You need to assign instances of classes to references for them to work.
Replace
if(myType.Length == 0)myType = new MyClass[1];
with
if (myType.Length == 0) myType = new MyClass[1] { new MyClass() };
Now you will create a new MyClass array that actually contains a live instance of MyClass, instead of null.
Your Solution Only Works In C# :D, I Need Both, I Think JavaScript Can't Create new Array Classes Like C#, The Problem Is With The Brackets.
Why do you need both?
Then replace
if(myType.Length == 0)myType = new $$anonymous$$yClass[1];
With
if(myType.Length == 0)
{
myType = new $$anonymous$$yClass[1];
myType[0] = new $$anonymous$$yClass();
}
$$anonymous$$y Assets And Packages That Get Uploaded Have To Be In Both Languages To Be Readable And Used By Everyone, Also Thank You So $$anonymous$$uch, It Worked!
Follow this Question
Related Questions
Need a hand using array.Length in a C# editor script 1 Answer
Array.length edited using scripts 2 Answers
Not a member of Object..Can't get class.name from array 1 Answer
C# custom class array error in adding a new entry. 1 Answer
Pushing GameObject into JS Array returns NullReferenceException 1 Answer