Search Unity

Argument Exception: "An element with the same key already exists in the dictionary."

Discussion in 'Scripting' started by Mohoruto, Feb 2, 2020.

  1. Mohoruto

    Mohoruto

    Joined:
    Feb 2, 2020
    Posts:
    5
    Hi.

    So, I've recently finished the Text Based Adventure Game tutorial on Unity's YouTube channel, and decided to extend upon it myself. The game uses dictionaries to handle where the player can navigate to, for example "go north", which will then go north given that the direction is available. Currently, in my version, I have two different rooms which offer the direction 'west' in which the player would type "go west" to travel to a different room. I can go west in the first encounter, however, later on in another room where the player can go west.. When you type 'go west' I get an error displaying:

    ArgumentException: An element with the same key already exists in the dictionary.
    System.Collections.Generic.Dictionary`2[System.String,Room].Add (System.String key, .Room value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:404)
    RoomNavigation.UnpackExitsInRoom () (at Assets/Scripts/RoomNavigation.cs:22)
    GameController.UnpackRoom () (at Assets/Scripts/GameController.cs:52)
    GameController.DisplayRoomText () (at Assets/Scripts/GameController.cs:41)
    RoomNavigation.AttemptToChangeRooms (System.String directionNoun) (at Assets/Scripts/RoomNavigation.cs:33)
    Go.RespondToInput (.GameController controller, System.String[] seperatedInputWords) (at Assets/Scripts/Go.cs:10)
    TextInput.AcceptStringInput (System.String userInput) (at Assets/Scripts/TextInput.cs:32)
    UnityEngine.Events.InvokableCall`1[System.String].Invoke (System.String args0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:207)
    UnityEngine.Events.UnityEvent`1[T0].Invoke (.T0 arg0) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:58)
    UnityEngine.UI.InputField.SendOnSubmit () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:1590)
    UnityEngine.UI.InputField.DeactivateInputField () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:2306)
    UnityEngine.UI.InputField.OnUpdateSelected (UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/InputField.cs:1262)
    UnityEngine.EventSystems.ExecuteEvents.Execute (IUpdateSelectedHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:99)
    UnityEngine.EventSystems.ExecuteEvents.Execute[IUpdateSelectedHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
    UnityEngine.EventSystems.EventSystem:Update()


    Could anybody help me as to why this is happening? Thanks for any assistance.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I mean...it's a pretty clear error. You have a dictionary with a key value pair and you're trying to add another entry that uses the same key name.

    So if I have "go west", "room113" and then I try to add "go west", "room 115" to the same dictionary, I am attempting to have two entries with the same key name. The dictionary would have no idea which one I wanted if I asked it to return one of the values.

    You'll either have to rethink how you're setting things up or you need to modify the value part if the key already exist.
     
  3. Mohoruto

    Mohoruto

    Joined:
    Feb 2, 2020
    Posts:
    5
    Hi, thanks for the reply. I think it's worth mentioning that after navigating to a room, the dictionary is cleared. Thus, removing the need for adding multiple entries. I have tried to create different rooms other than the one I am having problems with and the player is able to navigate west in those other rooms.. But not in the one I am having issues with. I would have solved it if this simply was the case. Perhaps I am missing something in the inspector, but I'm not entirely sure.
     
  4. Mohoruto

    Mohoruto

    Joined:
    Feb 2, 2020
    Posts:
    5
    After some fiddling around, I managed to fix the problem. It seems like it was something in the inspector, like an incorrect game object reference.