I’m using Unity 5.0.1 64bit on windows 7 64bit.
I have this class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace CharacterCustomizer{
[System.Serializable]
public class SerialDictionary<Key, Value>: Dictionary<Key, Value>, ISerializationCallbackReceiver{
[SerializeField]
List<Key> serialKeys = new List<Key>();
[SerializeField]
List<Value> serialValues = new List<Value>();
public void OnAfterDeserialize(){
base.Clear();
if(serialKeys.Count != serialValues.Count){
throw new System.Exception(
string.Format("there are {0} keys and {1} values after deserialization. Lengths do not match.")
);
}
for (int i = 0; i < serialKeys.Count; i++){
Add(serialKeys[i], serialValues[i]);
}
}
public void OnBeforeSerialize(){
serialKeys.Clear();
serialValues.Clear();
var tmpKeys = base.Keys;
//
foreach(var curKey in tmpKeys){
//foreach(var keyVal in this){
serialKeys.Add(curKey);
serialValues.Add(base[curKey]);
//serialKeys.Add(keyVal.Key);
//serialValues.Add(keyVal.Value);
}
}
}
}
Problem: when I change source code a bit in monodevelop AND unity editor reloads script, the class starts crashing furiously at this line:
public void OnBeforeSerialize(){
.....
foreach(var curKey in tmpKeys){//<== here
....
With this exception:
And values start disappearing from dictionary. I’ve noticed similar problems with dictionaries in few other places.
The behavior continues till I reload the scene.
How do I fix this?