Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Subscribing to PlayerInput C# events doesnt work for me

Discussion in 'Input System' started by Roboserg, Sep 12, 2021.

  1. Roboserg

    Roboserg

    Joined:
    Jun 3, 2018
    Posts:
    83
    New input system, was following a blog post tutorial. But for me the OnJump method is not called at all, I don't see the Debug Log "OnJump". Any thoughts?

    Code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class InputManager : MonoBehaviour
    8. {
    9.     private void Awake()
    10.     {
    11.         GetComponent<PlayerInput>().onActionTriggered += OnJump;
    12.         GetComponent<PlayerInput>().onControlsChanged += OnDeviceChanged;
    13.     }
    14.  
    15.     private void Start()
    16.     {
    17.         BroadcastMessage("receiveMsg", "test", SendMessageOptions.DontRequireReceiver);
    18.     }
    19.  
    20.     private void OnJump(InputAction.CallbackContext context)
    21.     {
    22.         Debug.Log("OnJump");
    23.         if (context.action.name != "Jump") return;
    24.         Debug.Log("Jump");
    25.         GetComponent<Rigidbody>().AddForce(Vector3.up * 50);
    26.     }
    27.  
    28.     private void OnDeviceChanged(PlayerInput obj)
    29.     {
    30.         Debug.Log("Device now is: " + obj.currentControlScheme);
    31.     }
    32. }
    33.  
     
  2. DAcKey

    DAcKey

    Joined:
    Feb 11, 2019
    Posts:
    9
    use callback.action without .name (idk why...)