Hey and Hello,
I have problem about calling some function in my script form Custom Editor.
There are 2 scripts in my test project, one is the Custom Editor script and the second one is some very primitive class, here is the code:
I tried to simplify the code to visualize the problem.
Editor:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class myCustomEditor : EditorWindow
{
[MenuItem("Edit/=== MY TEST EDITOR ===")]
static void Init()
{
EditorWindow.GetWindow(typeof(myCustomEditor), false, "Test");
}
// size
int width = 5;
int height = 5;
void OnGUI()
{
// width slider
EditorGUILayout.LabelField("Width:");
width = EditorGUILayout.IntSlider(width, 1, 10);
// height slider
EditorGUILayout.LabelField("Height:");
height = EditorGUILayout.IntSlider(height, 1, 10);
// spacing
GUILayout.Space(15.0f);
// generate button
if (GUILayout.Button("Create"))
{
GameObject root = new GameObject();
root.AddComponent<boxScript>();
boxScript bs = root.GetComponent<boxScript>();
bs.makeArray(width, height);
// check
Debug.Log(bs.arr.Length);
}
}
}
Primitive Class:
using UnityEngine;
using System.Collections;
public class boxScript : MonoBehaviour {
// array
public int width;
public int height;
public int [,] arr;
// make
public void makeArray(int w, int h)
{
width = w;
height = h;
arr = new int [h,w];
}
// test if worked
void Start()
{
Debug.Log(arr.Length);
}
}
Debug.Log(bs.arr.Length); in the “myCustomEditor” script works just fine, it shows proper length of the array, however when I enter ‘Play Mode’ I get the “NullReferenceException” from Start() function of the boxScript, as if the array didn’t exist.
Why is that happening? Why only “width” and “height” variables make it to the Game Mode, and is there any way to achieve what I’m trying to do?
The main goal is to generate array in Custom Editor Script and assign it to some other script, or tell that script to generate it based on some input(like in the example above), all in Edit-Mode, before actual game play.