editor script resetting variables when play mode is triggered

Sorry to bump this, but I’ve been trying for almost a month with no success and I’d rather bump than start a new topic…I still have no idea why the array is losing all references to the spawned GameObjects when play mode is triggered.

Just as the topic suggests, I have a script running in the editor, which creates a number of gameObjects and then stores references to them in an array within the script.

When I run the script, the custom inspector window I made to view and edit them gets really small and the slider controls and such disappear(which normally occurs when it tries to access an out-of-bounds array index.) This persists until I exit play mode and actually run the script to create the objects again (though if I don’t delete the existing ones I get duplicates.)

Is there any way to keep Unity from reloading the script and losing all the information I have stored in memory at the time?

Thanks

EDIT:

This is my entire inspector script, its a very simple prototype of what I eventually want it to look like, but I doubt that this is the issue.

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(HexManager))]
public class HexInspector : Editor {

    int xCoordinate = 0;
    int yCoordinate = 0;

        

    public override void  OnInspectorGUI()
    {
        HexManager script=(HexManager) target;
        

        DrawDefaultInspector();


      
        EditorGUILayout.Space();

         if (script.showEditor)
        {

            int minX;
            //Debug.Log(script.xDimension + " " + script.yDimension);
            yCoordinate = EditorGUILayout.IntSlider("Y Coordinate", yCoordinate, 0, script.grid.GetLength(1)-1);


            minX = yCoordinate;
            //if odd numbered row, is incremented to produce correct hex coordinates
            if (minX % 2 == 1)
            {
                minX++;
            }

            //converting orthagonal grid coordinates into non-orthagonal hex coordinates
            minX = 0 - (minX / 2);


            xCoordinate = EditorGUILayout.IntSlider("X Coordinate", xCoordinate, minX, script.grid.GetLength(0)-1 + minX);

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Node at","("+xCoordinate + "," + yCoordinate + ")");
            EditorGUILayout.Space();

            script.grid[xCoordinate - minX, yCoordinate].passValue = EditorGUILayout.FloatField(
                "PassValue", script.grid[xCoordinate - minX, yCoordinate].passValue);


        } 

         EditorGUILayout.Space();
         EditorGUILayout.Space();
         EditorGUILayout.Space();
         EditorGUILayout.Space();
         EditorGUILayout.TextArea ("THIS OPERATION CANNOT BE UNDONE, CURRENT GRID WILL BE LOST");

        if (GUILayout.Button("Create Grid"))
        {
            
            script.SpawnGrid();
            xCoordinate = 0;
            yCoordinate = 0;
        }
       
    }
}

And this is the code I use to instantiate and assign the components of the instantiated gameObjects to the array(its very sloppy, I know…I’m not worrying about optimizations until I have it working correctly.)

    while (currentX < xDimension)
{
        GameObject temp;

        
        if (drawGrid)
        {
            temp = (GameObject)Instantiate(hex, spawnPos, transform.rotation);
            temp.transform.localScale = new Vector3(distanceBetween,distanceBetween,distanceBetween);
            temp.name = xNum.ToString() + "," + yNum.ToString();
            temp.transform.parent = this.transform;
            tempNode = temp.GetComponent<HexNode>();
            tempNode.coordinates = new Vector2(currentX, currentY);
            tempNode.myController = this;
            grid[currentX,currentY]=tempNode;

    
        }
    }

I’m still not sure why the array of hexNodes (grid[,]) is being reset each time I enter and leave play mode…I really have no idea what else to try with this, but it’s really becoming a bottleneck with my production right now.

You need to mark the fields that you want saved/kept-around with [SerializeField]. Make sure the fields are of a Serializable type; I had a List<custom_class> that I needed to be saved and I had to mark the class [System.Serializable].,You need to mark the fields that you want saved or kept-around with [SerializeField]. Make sure the field is a Serializable type; I had a List of a custom class and I had to mark the class [System.Serializable].

As well as serializing everything, make sure you

EditorUtility.SetDirty(myScriptThatWantsSaving)

afterwards