Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can not bind to Unity Events in Player Input

Discussion in 'Input System' started by MaxAuer, Aug 30, 2019.

  1. MaxAuer

    MaxAuer

    Joined:
    Jul 14, 2017
    Posts:
    3
    Hi!

    Trying out the new Input System but already struggeling to even bind the Player Input to a Unity Event. I am using the newest Beta (2019.3.0b1) and Input System (0.9.5).

    If I select Invoke Unity Events on the Player Input component I can not select my public function.

    Here is the code:
    Code (CSharp):
    1. public class TestInput : MonoBehaviour
    2. {
    3.     public void OnMove(InputAction.CallbackContext context)
    4.     {
    5.         Debug.Log(context.ReadValue<Vector2>());
    6.     }
    7. }
    Here is an overview of the UI. I can not select the action.
    Am I missing something obvious?

    Unbenannt.png
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Unfortunately, this is a bug in 2019.3. Also affects uGUI. I'd expect it'll get fixed as the 2019.3 beta comes along.
     
    dnsl48 and GilbertoBitt like this.
  3. MaxAuer

    MaxAuer

    Joined:
    Jul 14, 2017
    Posts:
    3
    I was already doubting myself!

    Thanks for the information.
     
  4. ajay_birla

    ajay_birla

    Joined:
    Apr 29, 2016
    Posts:
    2
    We're facing the same issue in 2019.2.5f1. Any FIX for this specific version of Unity?
     
  5. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Fixed in latest 2019.2.
     
  6. Deleted User

    Deleted User

    Guest

    Can someone confirm this is still broken on 2019.3.0f6, with the Input System package at preview.4 - 1.0.0? All I see is "No Function", and I'm as barebones as the above example.
    0.png 1.png 2.png
     
    Last edited by a moderator: Feb 5, 2020
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    It says "MonoScript" there, which means that you've assigned the TestInput script asset instead of the one on your object.

    I made the same mistake on GGJ last weekend, I think it's because I named the script and the object the script was on the same thing, then I selected the UnityEvent's assignment nipple and wrote that name in, not realizing it was in asset mode.

    @Rene-Damm, it seems strange to be able to assign an asset to receive the callback from PlayerInput. I guess it's not impossible that it'd be relevant for someone, but the glue code to make that work isn't bad enough that it's very worthwhile as a feature. Or can't you set UnityEvents to not target assets?
     
    bi23tl, jdivs, Beguiled and 7 others like this.
  8. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Good question. Agree it's strange that's possible. Wasn't even aware.
     
  9. BoxThatBeat

    BoxThatBeat

    Joined:
    Jan 22, 2016
    Posts:
    1
    Isn't this just a limitation of the Unity EventSystem where it only allows functions to have the basic types as parameters. So any function that doesnt follow that rule will not show up. That makes me wonder how I am supposed to get the value for a move function though.
     
    OfAllTheBars and Apahdos like this.
  10. OfAllTheBars

    OfAllTheBars

    Joined:
    Dec 25, 2016
    Posts:
    1
    In 2019.3.15f1 you can assign a method with a InputAction.CallbackContext parameter...

    upload_2020-5-30_9-54-22.png

    My method (for testing) is as simple as:

    upload_2020-5-30_9-55-24.png
     
    Shaelle likes this.
  11. Trunex

    Trunex

    Joined:
    Aug 17, 2019
    Posts:
    9
    I use Unity 2019.3.15f1 and the functions do not appear to me either.

    upload_2020-6-7_5-48-46.png

    upload_2020-6-7_5-49-31.png
     
  12. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    Looks like you have the wrong object selected as the target. Replace the PlayerInput component selected there with a reference to your script component.

    upload_2020-6-9_12-8-2.png
     
  13. Trunex

    Trunex

    Joined:
    Aug 17, 2019
    Posts:
    9
    It's true, my fault I'm sorry. It works perfectly. Thank you.
     
  14. sylvestre6598

    sylvestre6598

    Joined:
    Dec 11, 2018
    Posts:
    2
    Hello, I have troubles accessing my functions too. I might have missed something, tried everything on this topic but still fails to make it work.

    EDIT : I am on on Unity 2019.4.2f1 and 1.0.0 Input System versions

    See the screenshots as follows :
    upload_2020-7-16_0-46-45.png

    upload_2020-7-16_0-47-36.png

    I'd be very appreciative for any help but please try to break it down a bit as am still a newcomer on Unity.

    Thank you in advance.
     
  15. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    I believe only public methods can be picked in UnityEvents, not private ones.
     
  16. sylvestre6598

    sylvestre6598

    Joined:
    Dec 11, 2018
    Posts:
    2
    I think that's what I missed, i feel a bit dumb haha. I'll try it thanks.

    EDIT: Worked like a charm, thank you
     
    Last edited: Jul 16, 2020
  17. Keildelaman22

    Keildelaman22

    Joined:
    Feb 20, 2021
    Posts:
    1
    Hello i also have troubles with the Input System and hope someone can help me.

    It looks like Unity can not Invoke my Methods OnMove(), OnJump, etc and i dont know why. Here are some pictures:
    upload_2021-2-20_14-36-56.png

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.  
    7.     //PlayerControls controls;
    8.  
    9.     public PlayerMovement controller;
    10.     public float runSpeed = 40f;
    11.  
    12.     float move = 0f;
    13.     bool jump = false;
    14.  
    15.         private void Awake()
    16.         {
    17.             //controls = new PlayerControls();
    18.  
    19.             //controls.Gameplay.Move.performed += context => move = context.ReadValue<Vector2>().x * runSpeed;
    20.             //controls.Gameplay.Move.canceled += context => move = 0;
    21.             //controls.Gameplay.Jump.performed += context => jump = true;
    22.             //controls.Gameplay.Jump.canceled += context => jump = false;
    23.             //controls.Gameplay.Attack.performed += context => Attack();
    24.             //controls.Gameplay.SpecialAttack.performed += context => SpecialAttack();
    25.  
    26.         }
    27.     /*
    28.  
    29.         private void OnEnable()
    30.         {
    31.             controls.Gameplay.Enable();
    32.         }
    33.  
    34.         private void OnDisable()
    35.         {
    36.             controls.Gameplay.Disable();
    37.         }
    38.     */
    39.  
    40.     public void OnMove(InputAction.CallbackContext context)
    41.     {
    42.         move = context.ReadValue<Vector2>().x * runSpeed;
    43.         Debug.Log("Move" + move);
    44.     }
    45.  
    46.     public void OnJump(InputAction.CallbackContext context)
    47.     {
    48.         jump = context.ReadValue<bool>();
    49.         jump = context.action.triggered;
    50.         Debug.Log("Jump" + jump);
    51.     }
    52.  
    53.     public void OnAttack(InputAction.CallbackContext context)
    54.     {
    55.         Debug.Log("Attack");
    56.     }
    57.  
    58.     public void OnSpecialAttack(InputAction.CallbackContext context)
    59.     {
    60.         Debug.Log("Special Attack");
    61.     }
    62.     private void FixedUpdate()
    63.     {
    64.         controller.Move(move * Time.fixedDeltaTime, false, jump);
    65.         Debug.Log(move + " " + jump);
    66.     }
    67.  
    68.  
    69.  
    70.  
    71. }
    72.  
    [EDIT here are my Input Actions if it helps finding the Error]
    upload_2021-2-20_14-44-27.png
     
    Last edited: Feb 20, 2021
    nx-sm likes this.
  18. nx-sm

    nx-sm

    Joined:
    Jul 4, 2015
    Posts:
    10
    I had this issue recently where all behaviour types in Player Input wouldn't reach my methods. Try deleting old Input Action Asset > Restart Unity > Create new asset with the same setup.
     
    NPCsZ, Deleted User and GiftedMamba like this.
  19. Shaelle

    Shaelle

    Joined:
    Nov 19, 2018
    Posts:
    9
    Damn! That's what I was missing, thank you: I spend almost the whole day trying to figure out why the function disappears from the menu when I adding this parameter — while it merely moved to another part of menu. :)
     
  20. Deleted User

    Deleted User

    Guest

    Thank you so much! I was having a very similar issue and this fixed it. I wish I had found this yesterday as that would of saved me an hour.
     
  21. omony

    omony

    Joined:
    Apr 2, 2020
    Posts:
    6
    I had an issue with invoking C# / Unity events. Deleting and creating input actions as new helps - thanks for the advice. However, the real issue I guess was the namespace I set for the generated class.