- Home /
prefabs and IDs
In my game, I have made a prefab of an enemy object and that prefab has a script attached to it that creates a sort of ID for each object so that when there are multiple in the scene, there is a way to distinguish between them. I then load the IDs and references to their game objects into a dictionary (c#) to organize the data. This worked fine for a little while.
However, when I quit unity and reloaded my project, I can't run the game without getting an error saying that there already exists an element with the same key in the dictionary. I am using the size of the dictionary as the ID for each element, so theoretically, with each element added, the size increases by 1 and thus, when the next ID is assigned, it will have a different ID.
What I find really weird is that when I delete all of those objects and then add them back with the prefab, it all runs perfectly until quitting and loading unity again. Hopefully this was enough information to answer the question and thank you for any answers!
I think we will need to see a little bit of code.
It also sounds like you may be best served using a list rather than a dictionary, since they won't throw the exception.
Answer by Cherno · Aug 18, 2014 at 01:35 AM
Do you actually save the data from your dictionary? Any data not serialized is lost when exiting play mode.
Your answer