How to destroy on exiting PlayMode/EditMode?
Hi, Im new to the Unity framework, and having some issues.
Im instantiating an array of GameObjects from code in the Start().
Tiles = new GameObject[SizeX, SizeZ];
for (int x = 0; x < SizeX; x++)
{
for (int z = 0; z < SizeZ; z++)
{
var tile = (GameObject) Instantiate(TileTemplate, new Vector3(x, 0, z), Quaternion.identity);
tile.GetComponentInChildren<Renderer>().enabled = false;
Tiles[x, z] = tile;
}
}
Where TileTemplate is a public GameObject. The GameObjects are instantiating as they should, and Scripts are running fine.
The problem occurs when I´m exiting PlayMode. The GameObjects are persisting them selvs in the editor, and are duplicated the next time I start PlayMode.
One thing is that I dont totally understand why (Editor state-full?), but ok.
So I´m then looking for something like Stop() or or onApplicationStop() which do not exist, but find my way to
EditorApplication.playmodeStateChanged += PlaymodeStateChanged;
and
private void PlaymodeStateChanged()
{
if (!EditorApplication.isPlayingOrWillChangePlaymode &&
EditorApplication.isPlaying )
{
foreach (var tile in Tiles)
{
DestroyImmediate(tile);
//Or
Destroy(tile);
}
Tiles = null;
}
}
And its not working... the method is called and the foreach loop is running, but its not destroying the GameObjects.
So I wonder is my code faulty, or is there something fundamental of the Unity framework I got wrong?
Answer by Adam-Mechtley · Nov 28, 2016 at 09:31 AM
Hi! If something is instantiated in that way inside a MonoBehaviour's Start () function, then it should automatically get cleaned up when the editor stops playing. You don't have something like ExecuteInEditMode on your script, do you?
Yeah, that was the problem (y). But Im not quite getting why it happens?
I am doing a null check on the array before instantiating. So if the editor creates the objects, they should be stored in the array. Im expecting that the editor would destroy the class and the GameObjects when starting play-mode. Not dereferencing them and keeping them in the scene.
If they're instantiated in the editor when the application is not playing, then they will persist in the editor if they are not destroyed.
The problem here is that multidimensional arrays (i.e. your Tiles property) cannot be serialized, and so the data stored in it will not persist between domain reloads (i.e. playing/stopping editor) even though ExecuteInEdit$$anonymous$$ode is on. What you need to do is something like this:
[System.Serializable]
public class TileRow {
[SerializeField]
private List<Tile> m_Row = new List<Tile> ();
public TileColumn (Tile[] tiles) {
m_Row.AddRange (tiles);
}
public int Count { get { return m_Row.Count; } }
public Tile this[int index] {
get { return m_Tiles[index]; }
set { m_Tiles[index] = value; }
}
public void Add (Tile tile) { m_Tiles.Add (tile); }
public void RemoveAt (int index) { m_Tiles.RemoveAt (index); }
}
public class $$anonymous$$yClass : $$anonymous$$onoBehaviour {
[SerializeField]
private int m_SizeX = 0;
[SerializeField]
private int m_SizeZ = 0;
[SerializeField]
private List<TileRow> m_Tiles = new List<TileRow> ();
protected virtual void OnValidate () {
SynchronizeTiles ();
}
protected virtual void Start () {
SynchronizeTiles ();
}
private void SynchronizeTiles () {
while (m_Tiles.Count < m_SizeZ)
m_Tiles.Add (new TileRow (new Tile[m_SizeX]));
while (m_Tiles.Count > m_SizeZ) {
foreach (var tile in m_Tiles[m_Tiles.Count - 1]) {
if (tile != null)
DestroyImmediate (tile);
}
m_Tiles.RemoveAt (m_Tiles.Count - 1);
}
for (int x = 0; z < m_SizeZ; ++z) {
while (m_Tiles[z].Count < m_SizeX)
m_Tiles[z].Add (null);
while (m_Tiles[z].Count > m_SizeX) {
int i = m_Tiles[z].Count - 1;
if (m_Tiles[z][i] != null)
DestroyImmediate (m_Tiles[z][i]);
m_Tiles[z].RemoveAt (i);
}
for (int z = 0; x < m_SizeX; ++x) {
if (m_TilePrefab == null) {
if (m_Tiles[z][x] != null)
DestroyImmediate (m_Tiles[z][x]);
m_Tiles[z][x] = null;
}
else {
if (m_Tiles[z][x] == null)
m_Tiles[z][x] = (Tile)Instantiate (m_TilePrefab, new Vector3 (x, 0f, z), Quaternion.identity);
}
if (m_Tiles[z][x] != null)
m_Tiles[z][x].GetComponentInChildren<Renderer>().enabled = false;
}
}
}
}
O$$anonymous$$, TIL: Unity relays heavily on serialization.
But do I have to declare types explicit as serializable ? I thought that was implicit when a class has the System.Serializable attribute.
Btw, is there a Wiki / reffrence page on Unity Internals ?
Your answer
Follow this Question
Related Questions
Instantiate and then destroy a prefab in a list. C# 1 Answer
What is Casting in Unity ? 1 Answer
RPG instantiate problem 3 Answers
Game Object doesn't instantiate at Mouse Position 3 Answers
The bullet never destroyed!!! 2 Answers