Custom Editor - Call Component's Method [Problem]

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.

You should use EditorUtility.SetDirty() to tell Unity save modified variables.
Like so:

if (GUILayout.Button("Create"))
{
    GameObject root = new GameObject();
    root.AddComponent<boxScript>();
    boxScript bs = root.GetComponent<boxScript>();
    bs.makeArray(width, height);
    EditorUtility.SetDirty(bs);
}

Start will complain if the array hasn’t been initialized, which won’t be the case until you run MakeArray().

here are my versions of your original scripts.

using UnityEngine;
using UnityEditor;

public class MyCustomEditor : EditorWindow
{
    [MenuItem("Edit/=== MY TEST EDITOR ===")]
    public static void Init()
    {
        GetWindow(typeof(MyCustomEditor), false, "Test");
    }

    private int _width = 5;
    private int _height = 5;
    private GameObject _root;

    public void OnGUI()
    {
        CreateSlider("Width:", ref _width, 1, 10);
        CreateSlider("Height:", ref _height, 1, 10);

        GUILayout.Space(15.0f);
 
        if (GUILayout.Button("Create"))
        {
            if (_root == null) { _root = new GameObject(); }
            var bs = _root.GetComponent<BoxScript>() ?? _root.AddComponent<BoxScript>();
            bs.MakeArray(_width, _height);
        }

        if (_root != null)
        {
            var bs = _root.GetComponent<BoxScript>();

            if ((bs != null) && (bs.arr != null) && (bs.width == _width) && (bs.height == _height))
            {
                RenderArray(bs);
                Repaint();
            }
        }
    }

    private void CreateSlider(string valLabel, ref int val, int valMin, int valMax)
    {
        EditorGUILayout.LabelField(valLabel);
        val = EditorGUILayout.IntSlider(val, valMin, valMax);
    }

    private void RenderArray(BoxScript bs)
    {
        for (var y = 0; y < bs.height; y++)
        {
            var row = "";
            for (var x = 0; x < bs.width; x++) { row += string.Format("{0:X4} ", bs.arr[x, y]); }
            GUILayout.Label(row);
        }
    }
}

using UnityEngine;
using System;
 
public class BoxScript : MonoBehaviour
{
    public int width, height;
    public int[,] arr;
 
    public void MakeArray(int w, int h)
    {
        width = w;
        height = h;

        if (arr == null)
        {
            arr = new int[width, height];
        }
        else
        {
            arr = (int[,]) ResizeArray(arr, new[] {width, height});
        }
    }

    // Resize array (From MSDN)
    private static Array ResizeArray(Array arr, int[] newSizes)
    {
        if (newSizes.Length != arr.Rank) { throw new ArgumentException("array must have the same number of dimensions as there are elements in newSizes", "newSizes"); }
        var temp = Array.CreateInstance(arr.GetType().GetElementType(), newSizes);
        var length = (arr.Length <= temp.Length) ? arr.Length : temp.Length;
        Array.ConstrainedCopy(arr, 0, temp, 0, length);
        return temp;
    } 

    public void Start()
    {
        if (arr != null)
        {
            Debug.Log(arr.Length);
        }
    }
}