- Home /
Unity Crash on Adding to List Array, bug?
Hey guys, I am creating a custom editor and I have a list array of custom class. What I want to do is empty that array and dump a new array into the original one.
What I am doing:
//serialize original list array
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("Assets/Resources/LevelGFX_" + currentLevelID.ToString() + ".bin", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
formatter.Serialize(stream, test.Stages);
stream.Close();
//retrieve previously serialized list array and insert into obj variable
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("Assets/Resources/LevelGFX_" + currentLevelID.ToString() + ".bin", FileMode.Open, FileAccess.Read, FileShare.Read);
List<CStage> obj = (List<CStage>) formatter.Deserialize(stream);
stream.Close();
//clear original array and insert obj array into orignal array
foreach(CStage s in test.Stages.ToArray())
{
test.Stages.Remove(s);
}
foreach(CStage s in obj)
{
test.Stages.Add(s);
}
Here is the CStage Class:
using UnityEngine;
using System.Collections;
using System;
[Serializable]
public class CStage {
public string StageName = "New Stage";
public float StartTime = 0F;
public float EndTime = 150F;
public float SpeedMin = 1F;
public float SpeedMax = 2F;
public float MinSize = 2F;
public float MaxSize = 4F;
public float speed = 1F;
public float ZDepth = 5F;
public float Frequency = 2F;
public bool Sequential = false;
public float SequentialFudge = 2F;
public float RotationY = 0F;
[NonSerialized()] public GameObject EntryModel;
[NonSerialized()] public GameObject[] InbetweenModels = new GameObject[5];
[NonSerialized()] public GameObject ExitModel;
public bool FlipRandomally = false;
public bool RandomizeRotation = false;
[NonSerialized()] public Color TintMin = Color.white;
[NonSerialized()] public Color TintMax = Color.red;
//pos
[NonSerialized()] public Vector2 XminMax = new Vector2(-5F, 7F);
[NonSerialized()] public Vector2 YminMax = new Vector2(-4F, 4F);
[NonSerialized()] public Vector2 ZminMax = new Vector2(0F, 8F);
//internal
public bool selected = false;
public int gridY = 0;
public int buttonWidth = 100;
public int buttonHeight = 60;
public bool modelsFoldout = false;
public bool additionalFoldout = false;
public bool stopped = false;
public float rendererx = 5F;
//test
public GUIStyle gstyle = new GUIStyle();
public bool colorSet = false;
//state
public enum stateM
{Middle, Entry, End}
public stateM state = stateM.Entry;
}
However this causes Unity to crash. Any ideas will be greatly appreciated!
Comment
test.Stages.clear();
foreach(CStage s in obj)
{
test.Stages.Add(s);
}
clear list and add ins$$anonymous$$d of removing in a fashion that doesnt sound good. you are doing ToArray() in for each any specific reasons?
I did ToArray in clear because otherwise it would give me this error: InvalidOperationException: Collection was modified; enumeration operation may not execute.
Yeah I gave it a try but the error remains, the problem isnt in clear but in adding the obj array to the original one.