- Home /
Keeping object between edit and play modes.
I have an object with a multi-dimensional array of game objects that is initialized and filled in a function called from a custom inspector during edit mode. As soon as I press play the object is destroyed and rebuilt, which makes the array uninitialized/empty again.
I call 'DontDestroyOnLoad(this.gameObject)' in the object's awake function, but this doesn't seem to be doing anything. How can I preserve this array between edit and play modes? Thanks.
Edit: Here is the initialization for the array I'm trying to keep around. The 'fillArray()' function is called on a button press from my custom inspector. I noticed no difference after adding [System.Serializable] and [SerializeField]
using UnityEngine; using System.Collections; using System.Collections.Generic;
[System.Serializable] public class ArrayTest : MonoBehaviour { //Array size variables int iLength, iWidth, iHeight;
//A 3D array of game objects to be filled
[SerializeField]
GameObject[,,] goPrefabs;
//Prefab of game object to put in 3D array
public GameObject goPrefab;
public void fillArray()
{
iLength = (int)(transform.localScale.x);
iWidth = (int)(transform.localScale.z);
iHeight = (int)(transform.localScale.y);
goPrefabs = new GameObject[iLength, iHeight, iWidth];
//Fill the array with game objects
for(int i = 0; i < iLength; i++)
{
for(int j = 0; j < iHeight; j++)
{
for(int k = 0; k < iWidth; k++)
{
GameObject goCurrentObject = Instantiate(goPrefab) as GameObject;
goPrefabs[i, j, k] = goCurrentObject;
}
}
}
}
Answer by Jesse Anders · Apr 03, 2011 at 06:28 AM
Make sure the array is serializable (either public, or marked with the SerializeField attribute). You might also need to mark the object as 'dirty' from the editor script, although I can never remember exactly how that's supposed to work.
If that doesn't help, maybe you could post the variable declaration for the array.
I added the System.Serializable and SerializeField keywords to no effect. I'll post some code up to better illustrate what I'm talking about.
Is 'goPrefabs' the array you're wanting to serialize? I'm not sure if Unity directly supports serialization of multi-dimensional arrays. (There are workarounds though - for example, you can store the data as a 1-d array and then interpret it as a 2-d array.)
Yes goPrefabs is the array I want to serialize. Would you $$anonymous$$d going into more detail about the workaround you mentioned? It sounds like exactly what I need. Thanks!
There's more than one way to do it (you might find some examples if you search the forums for e.g. 'serialize 2-d array'). But, one method is to write a wrapper function to index a 1-d array as a 2-d array. Generally, converting 2-d indices to a single 1-d index will look something like this: index = row * num_columns + column (or the other way around, depending on your storage preference). Another approach that might work would be to create an array of serializable objects, each of which contains one row or column of the array and is itself serializable.
Ha I have interpreted my 3D array as a 1D array and as a result all of the array items are serialized but the array itself is still destroyed. I'll kick around the forums for another solution. Thanks for all the help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613120421im_/https://answers.unity.com/themes/thub/images/avi.jpg)