Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

New Unity Input System InvalidCastException error

Discussion in 'Input System' started by AgentFox101, Jan 21, 2019.

  1. AgentFox101

    AgentFox101

    Joined:
    Jun 6, 2018
    Posts:
    4
    Hi I'm new to the new Input System and I'm trying to make a simple move mechanic but I keep getting the same 2 errors:

    InvalidCastException thrown during execution of 'Performed' callback on action 'lightSaberControls/LightsaberMovement'
    UnityEngine.Experimental.Input.LowLevel.<>c__AnonStorey0:<>m__0(NativeInputUpdateType, Int32, IntPtr)
    UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, Int32, IntPtr) (at C:/buildslave/unity/build/Modules/Input/Private/Input.cs:91)


    and

    InvalidCastException: Cannot cast from source type to destination type.
    UnityEngine.Experimental.Input.InputActionMapState.ReadValue[Single] (Int32 bindingIndex, Int32 controlIndex, Boolean ignoreComposites) (at Library/PackageCache/com.unity.inputsystem@0.1.2-preview/InputSystem/Actions/InputActionMapState.cs:1173)
    UnityEngine.Experimental.Input.InputActionMapState.ReadCompositePartValue[Single] (Int32 bindingIndex, Int32 partNumber) (at Library/PackageCache/com.unity.inputsystem@0.1.2-preview/InputSystem/Actions/InputActionMapState.cs:1197)
    UnityEngine.Experimental.Input.InputBindingCompositeContext.ReadValue[Single] (Int32 partNumber) (at Library/PackageCache/com.unity.inputsystem@0.1.2-preview/InputSystem/Actions/InputBindingCompositeContext.cs:22)
    UnityEngine.Experimental.Input.Composites.DpadComposite.ReadValue (UnityEngine.Experimental.Input.InputBindingCompositeContext& context) (at Library/PackageCache/com.unity.inputsystem@0.1.2-preview/InputSystem/Actions/Composites/DpadComposite.cs:36)
    UnityEngine.Experimental.Input.InputActionMapState.ReadValue[Vector2] (Int32 bindingIndex, Int32 controlIndex, Boolean ignoreComposites) (at Library/PackageCache/com.unity.inputsystem@0.1.2-preview/InputSystem/Actions/InputActionMapState.cs:1150)
    UnityEngine.Experimental.Input.InputAction+CallbackContext.ReadValue[Vector2] () (at Library/PackageCache/com.unity.inputsystem@0.1.2-preview/InputSystem/Actions/InputAction.cs:698)
    Indicator.HandleMoveSaber (CallbackContext context) (at Assets/Game Elements/Nodes/Scripts/Indicator.cs:56)
    UnityEngine.Experimental.Input.InputActionMapState.CallActionListeners (UnityEngine.Experimental.Input.InputActionMap actionMap, UnityEngine.Experimental.Input.Utilities.InlinedArray`1& listeners, UnityEngine.Experimental.Input.TriggerState& trigger) (at Library/PackageCache/com.unity.inputsystem@0.1.2-preview/InputSystem/Actions/InputActionMapState.cs:896)
    UnityEngine.Experimental.Input.LowLevel.<>c__AnonStorey0:<>m__0(NativeInputUpdateType, Int32, IntPtr)
    UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, Int32, IntPtr) (at C:/buildslave/unity/build/Modules/Input/Private/Input.cs:91)

    Here's my code:

    Code (CSharp):
    1. void Start()
    2.  
    3.     {
    4.         nodeCollided = null;
    5.         masterControl.lightSaberControls.LightsaberMovement.Enable();
    6.         masterControl.lightSaberControls.LightsaberMovement.performed += HandleMoveSaber;
    7.     }
    8.  
    9.     void HandleMoveSaber(InputAction.CallbackContext context)
    10.     {
    11.         Vector2 position = context.ReadValue<Vector2>();
    12.         Debug.Log("Moving: " + position);
    13.        
    14.     /*    xpos = position.x;
    15.         ypos = position.y;
    16.         transform.localPosition = position;*/
    17.     }

    Can anyone help me?
     
  2. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,387
    Hmm, I thought I had gotten rid of all blind casting on that path. Could you paste me your .inputactions file?

    My assumption is that the Dpad composite has a child binding that is indeed not a button/axis/key but it shouldn't just blindly throw an InvalidCastException but rather tell you where things went wrong.
     
  3. AgentFox101

    AgentFox101

    Joined:
    Jun 6, 2018
    Posts:
    4
    Hi!
    The .inputactions file type isn't supported for upload, so I'm posting a screenshot and the C sharp class that was generated.

    Capture.PNG

    Code (CSharp):
    1. // GENERATED AUTOMATICALLY FROM 'Assets/MasterControl.inputactions'
    2.  
    3. using System;
    4. using UnityEngine;
    5. using UnityEngine.Experimental.Input;
    6.  
    7.  
    8. [Serializable]
    9. public class MasterControl : InputActionAssetReference
    10. {
    11.     public MasterControl()
    12.     {
    13.     }
    14.     public MasterControl(InputActionAsset asset)
    15.         : base(asset)
    16.     {
    17.     }
    18.     private bool m_Initialized;
    19.     private void Initialize()
    20.     {
    21.         // lightSaberControls
    22.         m_lightSaberControls = asset.GetActionMap("lightSaberControls");
    23.         m_lightSaberControls_Push = m_lightSaberControls.GetAction("Push");
    24.         m_lightSaberControls_Movement = m_lightSaberControls.GetAction("Movement");
    25.         m_lightSaberControls_LightsaberMovement = m_lightSaberControls.GetAction("LightsaberMovement");
    26.         m_Initialized = true;
    27.     }
    28.     private void Uninitialize()
    29.     {
    30.         m_lightSaberControls = null;
    31.         m_lightSaberControls_Push = null;
    32.         m_lightSaberControls_Movement = null;
    33.         m_lightSaberControls_LightsaberMovement = null;
    34.         m_Initialized = false;
    35.     }
    36.     public void SetAsset(InputActionAsset newAsset)
    37.     {
    38.         if (newAsset == asset) return;
    39.         if (m_Initialized) Uninitialize();
    40.         asset = newAsset;
    41.     }
    42.     public override void MakePrivateCopyOfActions()
    43.     {
    44.         SetAsset(ScriptableObject.Instantiate(asset));
    45.     }
    46.     // lightSaberControls
    47.     private InputActionMap m_lightSaberControls;
    48.     private InputAction m_lightSaberControls_Push;
    49.     private InputAction m_lightSaberControls_Movement;
    50.     private InputAction m_lightSaberControls_LightsaberMovement;
    51.     public struct LightSaberControlsActions
    52.     {
    53.         private MasterControl m_Wrapper;
    54.         public LightSaberControlsActions(MasterControl wrapper) { m_Wrapper = wrapper; }
    55.         public InputAction @Push { get { return m_Wrapper.m_lightSaberControls_Push; } }
    56.         public InputAction @Movement { get { return m_Wrapper.m_lightSaberControls_Movement; } }
    57.         public InputAction @LightsaberMovement { get { return m_Wrapper.m_lightSaberControls_LightsaberMovement; } }
    58.         public InputActionMap Get() { return m_Wrapper.m_lightSaberControls; }
    59.         public void Enable() { Get().Enable(); }
    60.         public void Disable() { Get().Disable(); }
    61.         public bool enabled { get { return Get().enabled; } }
    62.         public InputActionMap Clone() { return Get().Clone(); }
    63.         public static implicit operator InputActionMap(LightSaberControlsActions set) { return set.Get(); }
    64.     }
    65.     public LightSaberControlsActions @lightSaberControls
    66.     {
    67.         get
    68.         {
    69.             if (!m_Initialized) Initialize();
    70.             return new LightSaberControlsActions(this);
    71.         }
    72.     }
    73.     private int m_MouseandKeyboardSchemeIndex = -1;
    74.     public InputControlScheme MouseandKeyboardScheme
    75.     {
    76.         get
    77.  
    78.         {
    79.             if (m_MouseandKeyboardSchemeIndex == -1) m_MouseandKeyboardSchemeIndex = asset.GetControlSchemeIndex("Mouse and Keyboard");
    80.             return asset.controlSchemes[m_MouseandKeyboardSchemeIndex];
    81.         }
    82.     }
    83.     private int m_GamepadSchemeIndex = -1;
    84.     public InputControlScheme GamepadScheme
    85.     {
    86.         get
    87.  
    88.         {
    89.             if (m_GamepadSchemeIndex == -1) m_GamepadSchemeIndex = asset.GetControlSchemeIndex("Gamepad");
    90.             return asset.controlSchemes[m_GamepadSchemeIndex];
    91.         }
    92.     }
    93. }
    94.  
    I apologize if I'm doing something really stupid, like I said, I'm new to this.

    Thanks for the help!
     
    Last edited: Jan 24, 2019
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,942
    You can open the .inputactions file (and most other internal Unity files) in a text editor and copy that here - as long as you have asset serialization set to text (which is default)
     
  5. AgentFox101

    AgentFox101

    Joined:
    Jun 6, 2018
    Posts:
    4
    Oh okay thanks!

    Here's the file

    Code (CSharp):
    1. {
    2.     "name": "MasterControl",
    3.     "maps": [
    4.         {
    5.             "name": "lightSaberControls",
    6.             "id": "1ef4e9e6-1432-4f33-84b2-d8b3453befbd",
    7.             "actions": [
    8.                 {
    9.                     "name": "Push",
    10.                     "id": "a14220ac-5b39-4b9d-ba30-e89a336e5d1d",
    11.                     "expectedControlLayout": "Button",
    12.                     "bindings": []
    13.                 },
    14.                 {
    15.                     "name": "Movement",
    16.                     "id": "db6124d2-149f-4d4a-992a-2fae440301c0",
    17.                     "expectedControlLayout": "",
    18.                     "bindings": []
    19.                 },
    20.                 {
    21.                     "name": "LightsaberMovement",
    22.                     "id": "d1ea5ee4-a9b0-42b5-ac6c-70b3585249a9",
    23.                     "expectedControlLayout": "Dpad",
    24.                     "bindings": []
    25.                 }
    26.             ],
    27.             "bindings": [
    28.                 {
    29.                     "name": "",
    30.                     "path": "<Keyboard>/f",
    31.                     "interactions": "PressAndRelease",
    32.                     "processors": "",
    33.                     "groups": "Mouse and Keyboard",
    34.                     "action": "Push",
    35.                     "chainWithPrevious": false,
    36.                     "isComposite": false,
    37.                     "isPartOfComposite": false,
    38.                     "modifiers": ""
    39.                 },
    40.                 {
    41.                     "name": "",
    42.                     "path": "<Gamepad>/rightStickPress",
    43.                     "interactions": "PressAndRelease",
    44.                     "processors": "",
    45.                     "groups": ";Gamepad",
    46.                     "action": "Push",
    47.                     "chainWithPrevious": false,
    48.                     "isComposite": false,
    49.                     "isPartOfComposite": false,
    50.                     "modifiers": ""
    51.                 },
    52.                 {
    53.                     "name": "KeyboardMove",
    54.                     "path": "Dpad",
    55.                     "interactions": "",
    56.                     "processors": "",
    57.                     "groups": "",
    58.                     "action": "Movement",
    59.                     "chainWithPrevious": false,
    60.                     "isComposite": true,
    61.                     "isPartOfComposite": false,
    62.                     "modifiers": ""
    63.                 },
    64.                 {
    65.                     "name": "up",
    66.                     "path": "<Keyboard>/w",
    67.                     "interactions": "Hold(duration=1,repeat),Tap",
    68.                     "processors": "",
    69.                     "groups": ";Mouse and Keyboard",
    70.                     "action": "Movement",
    71.                     "chainWithPrevious": false,
    72.                     "isComposite": false,
    73.                     "isPartOfComposite": true,
    74.                     "modifiers": ""
    75.                 },
    76.                 {
    77.                     "name": "down",
    78.                     "path": "<Keyboard>/s",
    79.                     "interactions": "Hold(duration=2,repeat)",
    80.                     "processors": "",
    81.                     "groups": ";Mouse and Keyboard",
    82.                     "action": "Movement",
    83.                     "chainWithPrevious": false,
    84.                     "isComposite": false,
    85.                     "isPartOfComposite": true,
    86.                     "modifiers": ""
    87.                 },
    88.                 {
    89.                     "name": "left",
    90.                     "path": "<Keyboard>/a",
    91.                     "interactions": "Hold(duration=2,repeat)",
    92.                     "processors": "",
    93.                     "groups": ";Mouse and Keyboard",
    94.                     "action": "Movement",
    95.                     "chainWithPrevious": false,
    96.                     "isComposite": false,
    97.                     "isPartOfComposite": true,
    98.                     "modifiers": ""
    99.                 },
    100.                 {
    101.                     "name": "right",
    102.                     "path": "<Keyboard>/d",
    103.                     "interactions": "Hold(duration=2,repeat)",
    104.                     "processors": "",
    105.                     "groups": ";Mouse and Keyboard",
    106.                     "action": "Movement",
    107.                     "chainWithPrevious": false,
    108.                     "isComposite": false,
    109.                     "isPartOfComposite": true,
    110.                     "modifiers": ""
    111.                 },
    112.                 {
    113.                     "name": "GamepadMove",
    114.                     "path": "Dpad",
    115.                     "interactions": "Hold(repeat)",
    116.                     "processors": "StickDeadzone(min=0.1,max=0.925)",
    117.                     "groups": "",
    118.                     "action": "Movement",
    119.                     "chainWithPrevious": false,
    120.                     "isComposite": true,
    121.                     "isPartOfComposite": false,
    122.                     "modifiers": ""
    123.                 },
    124.                 {
    125.                     "name": "up",
    126.                     "path": "<Gamepad>/leftStick/up",
    127.                     "interactions": "",
    128.                     "processors": "",
    129.                     "groups": ";Gamepad",
    130.                     "action": "Movement",
    131.                     "chainWithPrevious": false,
    132.                     "isComposite": false,
    133.                     "isPartOfComposite": true,
    134.                     "modifiers": ""
    135.                 },
    136.                 {
    137.                     "name": "down",
    138.                     "path": "<Gamepad>/leftStick/down",
    139.                     "interactions": "",
    140.                     "processors": "",
    141.                     "groups": ";Gamepad",
    142.                     "action": "Movement",
    143.                     "chainWithPrevious": false,
    144.                     "isComposite": false,
    145.                     "isPartOfComposite": true,
    146.                     "modifiers": ""
    147.                 },
    148.                 {
    149.                     "name": "left",
    150.                     "path": "<Gamepad>/leftStick/left",
    151.                     "interactions": "",
    152.                     "processors": "",
    153.                     "groups": ";Gamepad",
    154.                     "action": "Movement",
    155.                     "chainWithPrevious": false,
    156.                     "isComposite": false,
    157.                     "isPartOfComposite": true,
    158.                     "modifiers": ""
    159.                 },
    160.                 {
    161.                     "name": "right",
    162.                     "path": "<Gamepad>/leftStick/right",
    163.                     "interactions": "Hold",
    164.                     "processors": "StickDeadzone(max=1)",
    165.                     "groups": ";Gamepad",
    166.                     "action": "Movement",
    167.                     "chainWithPrevious": false,
    168.                     "isComposite": false,
    169.                     "isPartOfComposite": true,
    170.                     "modifiers": ""
    171.                 },
    172.                 {
    173.                     "name": "GamepadControls",
    174.                     "path": "Dpad",
    175.                     "interactions": "",
    176.                     "processors": "StickDeadzone(max=1)",
    177.                     "groups": "",
    178.                     "action": "LightsaberMovement",
    179.                     "chainWithPrevious": false,
    180.                     "isComposite": true,
    181.                     "isPartOfComposite": false,
    182.                     "modifiers": ""
    183.                 },
    184.                 {
    185.                     "name": "up",
    186.                     "path": "<Gamepad>/rightStick/up",
    187.                     "interactions": "",
    188.                     "processors": "StickDeadzone(max=1)",
    189.                     "groups": ";Gamepad",
    190.                     "action": "LightsaberMovement",
    191.                     "chainWithPrevious": false,
    192.                     "isComposite": false,
    193.                     "isPartOfComposite": true,
    194.                     "modifiers": ""
    195.                 },
    196.                 {
    197.                     "name": "down",
    198.                     "path": "<Gamepad>/rightStick/down",
    199.                     "interactions": "",
    200.                     "processors": "StickDeadzone(max=1)",
    201.                     "groups": ";Gamepad",
    202.                     "action": "LightsaberMovement",
    203.                     "chainWithPrevious": false,
    204.                     "isComposite": false,
    205.                     "isPartOfComposite": true,
    206.                     "modifiers": ""
    207.                 },
    208.                 {
    209.                     "name": "left",
    210.                     "path": "<Gamepad>/rightStick/left",
    211.                     "interactions": "",
    212.                     "processors": "StickDeadzone(max=1)",
    213.                     "groups": ";Gamepad",
    214.                     "action": "LightsaberMovement",
    215.                     "chainWithPrevious": false,
    216.                     "isComposite": false,
    217.                     "isPartOfComposite": true,
    218.                     "modifiers": ""
    219.                 },
    220.                 {
    221.                     "name": "right",
    222.                     "path": "<Gamepad>/rightStick/right",
    223.                     "interactions": "",
    224.                     "processors": "StickDeadzone(max=1)",
    225.                     "groups": ";Gamepad",
    226.                     "action": "LightsaberMovement",
    227.                     "chainWithPrevious": false,
    228.                     "isComposite": false,
    229.                     "isPartOfComposite": true,
    230.                     "modifiers": ""
    231.                 },
    232.                 {
    233.                     "name": "MouseandKeyboardControls",
    234.                     "path": "Dpad",
    235.                     "interactions": "",
    236.                     "processors": "StickDeadzone(max=1)",
    237.                     "groups": "",
    238.                     "action": "LightsaberMovement",
    239.                     "chainWithPrevious": false,
    240.                     "isComposite": true,
    241.                     "isPartOfComposite": false,
    242.                     "modifiers": ""
    243.                 },
    244.                 {
    245.                     "name": "up",
    246.                     "path": "<Keyboard>/i",
    247.                     "interactions": "",
    248.                     "processors": "StickDeadzone(max=0.2)",
    249.                     "groups": ";Mouse and Keyboard",
    250.                     "action": "LightsaberMovement",
    251.                     "chainWithPrevious": false,
    252.                     "isComposite": false,
    253.                     "isPartOfComposite": true,
    254.                     "modifiers": ""
    255.                 },
    256.                 {
    257.                     "name": "down",
    258.                     "path": "<Keyboard>/k",
    259.                     "interactions": "",
    260.                     "processors": "StickDeadzone(max=0.1)",
    261.                     "groups": ";Mouse and Keyboard",
    262.                     "action": "LightsaberMovement",
    263.                     "chainWithPrevious": false,
    264.                     "isComposite": false,
    265.                     "isPartOfComposite": true,
    266.                     "modifiers": ""
    267.                 },
    268.                 {
    269.                     "name": "left",
    270.                     "path": "<Keyboard>/j",
    271.                     "interactions": "",
    272.                     "processors": "StickDeadzone(max=0.1)",
    273.                     "groups": ";Mouse and Keyboard",
    274.                     "action": "LightsaberMovement",
    275.                     "chainWithPrevious": false,
    276.                     "isComposite": false,
    277.                     "isPartOfComposite": true,
    278.                     "modifiers": ""
    279.                 },
    280.                 {
    281.                     "name": "right",
    282.                     "path": "<Keyboard>/l",
    283.                     "interactions": "",
    284.                     "processors": "StickDeadzone(max=0.1)",
    285.                     "groups": ";Mouse and Keyboard",
    286.                     "action": "LightsaberMovement",
    287.                     "chainWithPrevious": false,
    288.                     "isComposite": false,
    289.                     "isPartOfComposite": true,
    290.                     "modifiers": ""
    291.                 }
    292.             ]
    293.         }
    294.     ],
    295.     "controlSchemes": [
    296.         {
    297.             "name": "Mouse and Keyboard",
    298.             "basedOn": "",
    299.             "bindingGroup": "Mouse and Keyboard",
    300.             "devices": [
    301.                 {
    302.                     "devicePath": "<Keyboard>",
    303.                     "isOptional": false,
    304.                     "isOR": false
    305.                 },
    306.                 {
    307.                     "devicePath": "<Mouse>",
    308.                     "isOptional": false,
    309.                     "isOR": false
    310.                 }
    311.             ]
    312.         },
    313.         {
    314.             "name": "Gamepad",
    315.             "basedOn": "",
    316.             "bindingGroup": "Gamepad",
    317.             "devices": [
    318.                 {
    319.                     "devicePath": "<Gamepad>",
    320.                     "isOptional": false,
    321.                     "isOR": false
    322.                 }
    323.             ]
    324.         }
    325.     ]
    326. }
     
  6. CherryFake

    CherryFake

    Joined:
    Dec 14, 2019
    Posts:
    3
    uhm is there a answer? I have got the same problem
     
  7. asmuro

    asmuro

    Joined:
    Nov 16, 2014
    Posts:
    1
    Hi, I had the very same problem and I solved it this way.

    I had this configuration => not working
    upload_2020-1-12_15-36-3.png

    Why is not working the Vector2 conversion? because I defined the ActionType of the Action JoystickMove as a Button, and then all the move actions will translate in single floats not in Vector2.

    The solution is in this order:
    - Remove all the Stick movements mapped
    - Change the ActionType of your Action to "Value" and "Any" as you can see in the screenshot below
    - Map only the stick of your control

    Now I have this configuration and => is working
    upload_2020-1-12_15-40-47.png

    I hope this solution can work for you also! :)
     

    Attached Files:

  8. berkninan

    berkninan

    Joined:
    Jan 7, 2020
    Posts:
    2
    An InvalidCastException is thrown when cast from one type to another type is not supported. In some reference type conversions , the compiler cannot determine whether a cast will be valid. It is because of the source type cannot be converted to the destination type , so the cast does not succeed.It is important to note that this exception is caused by programming error and should not be handled in a try/catch block; instead, the cause of the exception should be eliminated. C# provides the "is" operator to enable you to test for compatibility before actually performing a cast operation . It will checks if the runtime type of an expression result is compatible with a given type.
     
  9. lmantock

    lmantock

    Joined:
    Feb 25, 2018
    Posts:
    1
    I had the same problem after copying my project to a new folder, specifically:
    InvalidCastException: Specified cast is not valid. UnityEngine.InputSystem.Editor.InputAction Importer Editor.GetAsset () (at Library/PackageCache/com.unity.inputsystem@1.0.0/InputSystem/Editor/AssetImporter/InputActionImporterEditor.cs:84)


    None of the above solutions worked for me. Steps I took to resolve:
    1. Open old InputActions.cs file in text editor
    2. Delete old InputActions.cs and InputActions.inputactions file
    3. Right click in Project window and create a new Input Actions file
    4. Select the new Input Actions file, toggle generate C# class, open generated C# class in text editor
    5. Paste old InputActions.cs code into new generated C# class
     
unityunity