Unable to pass dictionary through scenes

Hello.

My game has a GameControl script and a Dictionary that should be carried through every scene of the game. For testing reasons, I currently add both scripts to every scene and have a:

    void Awake () {
        // This will destroy any existing GameControls and assure that info here is passed on to other scenes
        if(control == null)
        {
            DontDestroyOnLoad(gameObject);
            control = this;
        }
        else if(control!= this)
        {
            Destroy(gameObject);
        }
}

function for the GameControl. However, I’m unable to do have dictionary!=this, as the operand != cannot be used with dictionaries. So what happens is I keep getting the error message that I have two instances of it running (the game doesn’t crash, but still…). The error is as follows:

“ArgumentException: An element with the same key already exists in the dictionary.
System.Collections.Generic.Dictionary2[System.String,System.Collections.Generic.List1[System.Single]].Add (System.String key, System.Collections.Generic.List`1 value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:404)
createDictionary.createCountryDictionary () (at Assets/Scripts/createDictionary.cs:168)
createDictionary.Awake () (at Assets/Scripts/createDictionary.cs:33)”

Is there a way to destroy any pre-existing dictionaries on the loaded scene in the awake function?

Thank you.

So many things going wrong here it’s hard to know where to start. So in no particular order:

The operand != can only be used between two types that are the same, (or almost the same). In your case you are comparing this, which is a MonoBehaviour, and control, which is a dictionary.

I would suggest going and looking up a proper singleton implementation. Singletons are a common design pattern, and good examples for Unity are everywhere.

Then finally google why you shouldn’t use a singleton. It’s not a hard and fast rule, but you should be aware of what you are walking into.

1 Like

Thanks for your reply.

I understand that != can only be used on the same types. The code I provided is for the GameControl (control), which works perfectly fine. since it destroys other GameControls on Awake() if it finds them.

Instead of just bashing the question, couldn’t it be better maybe to help? I am aware that it won’t work on the dictionary, I was just hoping for another construct or way to write it so that I can carry it from one scene to another (ONLY WHILE TESTING, when I’m done I’ll delete the dictionary gameobjects on other scenes and just have it on my mainMenu when loading, that way it’ll carry on throughout the game).

This isn’t much of a problem since my dictionary doesn’t change at all (obviously), so having an instance of it run into another gives me an error message, but no major flaw to the game itself.

I wasn’t bashing your question at all. Your question simply had a multipart answer, which I gave.

So, it’s hard to tell with how much code you have shown there, but your dictionary is a member of your gameobject that isn’t shown in that singleton code, right?

As for using a singleton, near as I can tell it’s the most practical way to carry data between scenes, so I wouldn’t sweat having just one in your game.

1 Like

@Kiwasi Thank you for trying to help!

@SgtZdog Sorry for the very delayed answer, but actually I’ve got two gameobjects, one that holds the dictionary and one that holds the GameControl. Basically, there’s no problem at all with the singletons, I just keep getting error messages because I can’t seem to destroy the dicstionaries that already exist (as I carry the previous one to the next scene). This is more annoying than anything else, really. It doesn’t crash the game or otherwise cause any actual error.

Anyway, thanks for the answers guys, I guess I’ll just keep getting the error message until I’m actually done with the game and I can delete all the gameobjects in other scenes wth dictionaries.