Dictionary Object and DontDestroyOnLoad Issue

I have a Dictionary<GameObject, int[ ]> object and I persist it using the DontDestroyOnLoad, along with some other data. The other data (strings, ints) work as you would expect. However when I retrieve the Dictionary object I have issues. The “Count” property properly reports the number the of entries in the hash, BUT when I go to access them in a foreach loop I get this error:

"MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object."

obviously I can do the null check, but why is it null? It appears the Dictionary object was perserved, but it’s contents were not.

Any ideas? Anyone seen this?

Try adding DontDestroyOnLoad to the GameObjects as you add them.

The reason for this is that DontDestroyOnLoad will preserve objects within the Transform hierarchy, but it doesn’t understand what to do with a Dictionary.

When the object is not parented under the preserved object you need to manually specify each individual object to preserve.

Yeah I can’t do that, I will have to think of another way around it. If I do what you are suggesting then every time I go back to that scene the Game Objects will be duplicated.

That is not an issue with Adding it per object or using Dictionaries, but an issue with using DontDestroyOnLoad as a whole.

If you want to use DontDestroyOnLoad for Objects then you are going to have to manage their duplication on entering the same scene twice.

Best suggestion I can give is to store an unique ID on store-able objects. When initializing the level, check to see if the ID is contained within the dictionary already. If it is, Destroy the object when it’s created.

LoadLevel will always just load every level in the scene regardless of existing assets. Anything marked as DontDestroyOnLoad will be duplicated unless you specifically make a script to handle that duplication.

he …can u talk me on DontDestroyOnLoad issue