Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

unity ARkit error: 'ArgumentException:An element with the same key already exists in the dictionary'

Discussion in 'ARKit' started by rayHrx, Feb 16, 2018.

  1. rayHrx


    Nov 6, 2017
    I was running the FocusSquareScene from the ARkit example folder via ARkit remote on my Ipad. When the program detects a new plane, the following error occurred:

    ArgumentException: An element with the same key already exists in the dictionary.
    System.Collections.Generic.Dictionary`2[System.String,UnityEngine.XR.iOS.ARPlaneAnchorGameObject].Add (System.String key, UnityEngine.XR.iOS.ARPlaneAnchorGameObject value) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:404)
    UnityEngine.XR.iOS.UnityARAnchorManager.AddAnchor (ARPlaneAnchor arPlaneAnchor) (at Assets/UnityARKitPlugin/Plugins/iOS/UnityARKit/Helpers/UnityARAnchorManager.cs:31)
    UnityEngine.XR.iOS.UnityARSessionNativeInterface.RunAddAnchorCallbacks (ARPlaneAnchor arPlaneAnchor) (at Assets/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/UnityARSessionNativeInterface.cs:453)
    UnityEngine.XR.iOS.ARKitRemoteConnection.AddPlaneAnchor (UnityEngine.Networking.PlayerConnection.MessageEventArgs mea) (at Assets/UnityARKitPlugin/ARKitRemote/ARKitRemoteConnection.cs:139)
    UnityEngine.Events.InvokableCall`1[UnityEngine.Networking.PlayerConnection.MessageEventArgs].Invoke (System.Object[] args) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:189)
    UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637)
    UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773)
    UnityEngine.Events.UnityEvent`1[T0].Invoke (.T0 arg0) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_1.cs:53)
    UnityEngine.Networking.PlayerConnection.PlayerEditorConnectionEvents.InvokeMessageIdSubscribers (Guid messageId, System.Byte[] data, Int32 playerId) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Networking/PlayerConnection/PlayerEditorConnectionEvents.cs:66)
    UnityEditor.Networking.PlayerConnection.EditorConnection.MessageCallbackInternal (IntPtr data, UInt64 size, UInt64 guid, System.String messageId) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Networking/PlayerConnection/EditorConnection.cs:137)

    I know it is caused by inserting duplicating keys into dictionary and I noticed on the Hierarchy panel there are multiple new created objects with same 'key' when the project is running:

    Screen Shot 2018-02-16 at 11.29.38 AM.png
    And it leaves a lot of planes on the scene:

    Screen Shot 2018-02-16 at 11.30.18 AM.png

    ... I did not change anything in this example project and the same error happens in the other project I am working on using ARkit.

    I have to use ARkit to complete a project and any help will be greatly appreciated !!! Thank you !!!
  2. harperAtustwo


    Nov 15, 2016
    I get the same problem. using the unchanged unity remote scene on the device and the unchanged focusquare in editor from the Unity ARKitRemote package.
  3. Beloudest


    Mar 13, 2015
    I get this too, is it just a Unity Remote issue? Have any you got to the bottom of it? Thanks
  4. dustin_red


    Feb 7, 2018
    I had the same issue, a quick fix was to change UnityARKitPlugin/Plugins/iOS/UnityARKit/Helpers/UnityARAnchorManager.cs AddAnchor() to check if it already had an anchor of the same name.

    Change this:
    Code (CSharp):
    2. public void AddAnchor(ARPlaneAnchor arPlaneAnchor) {
    3.             GameObject go = UnityARUtility.CreatePlaneInScene (arPlaneAnchor);
    4.             go.AddComponent<DontDestroyOnLoad> ();  //this is so these GOs persist across scene loads
    5.             ARPlaneAnchorGameObject arpag = new ARPlaneAnchorGameObject ();
    6.             arpag.planeAnchor = arPlaneAnchor;
    7.             arpag.gameObject = go;
    8.             planeAnchorMap.Add (arPlaneAnchor.identifier, arpag);
    9. }
    To this:
    Code (CSharp):
    2. public void AddAnchor(ARPlaneAnchor arPlaneAnchor) {
    3.            if (!planeAnchorMap.ContainsKey(arPlaneAnchor.identifier)) {              
    4.                       GameObject go = UnityARUtility.CreatePlaneInScene (arPlaneAnchor);
    5.                       go.AddComponent<DontDestroyOnLoad> ();  //this is so these GOs persist across scene loads
    6.                       ARPlaneAnchorGameObject arpag = new ARPlaneAnchorGameObject ();
    7.                       arpag.planeAnchor = arPlaneAnchor;
    8.                       arpag.gameObject = go;
    9.                       planeAnchorMap.Add (arPlaneAnchor.identifier, arpag);
    10.            }
    11. }
    brunobaron likes this.