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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Call Jump into PlayerController script -> CS0407 Error

Discussion in 'Input System' started by Nizzo87, Apr 6, 2023.

  1. Nizzo87

    Nizzo87

    Joined:
    Dec 28, 2019
    Posts:
    7
    Hello all,

    I am trying to read my Jump into the PlayerController script from my GameInput script. The only thing that is not recognised is the return value from the new Unity input system. The animator is in the PlayerController script. That is why I only write in the PlayerController Update that I have received it.

    Could you help me?

    GameInput Script:
    Code (CSharp):
    1. public class GameInput : MonoBehaviour
    2. {
    3.     private PlayerInputActions playerInputActions;
    4.     private Rigidbody rb;
    5.     [SerializeField] private float jumpAmount = 7;
    6.  
    7.     private void Awake()
    8.     {
    9.         playerInputActions = new PlayerInputActions();
    10.         rb = GameObject.Find("PlayerCanvas").GetComponent<Rigidbody>();
    11.         playerInputActions.Player.Enable();
    12.         playerInputActions.Player.Jump.performed += Jump_performed;
    13.  
    14.     }
    15.  
    16.     public float Jump_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
    17.     {
    18.         rb.AddForce(Vector2.up * jumpAmount, ForceMode.Impulse);
    19.         float inputFloat = playerInputActions.Player.Jump.ReadValue<float>();
    20.         return inputFloat;
    21.     }
    Thank you :) Greetings, Nico.
     

    Attached Files:

  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,456
    What's the error?
     
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Always provide the full error text. Nobody remembers the error codes.

    Anyway, CS0407 means that you tried to assign a method to an incompatible delegate type. The input system expects you to provide a function that takes one argument (the context) and returns void. You are giving it a function that takes one argument (the context) and returns a float.

    These are incompatible. Make the function return void (and don't return anything from it) and it ought to work fine.

    If you want to save that float value for later use, you could assign it to a variable on the GameInput class.
     
    Nizzo87 likes this.
  4. Nizzo87

    Nizzo87

    Joined:
    Dec 28, 2019
    Posts:
    7
    Hi guys,
    Sorry I missed the error message. But thank you for giving me feedback anyway :) You're right. I changed the function to a void funtion and it works. Thank you very much.