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.

Bug Input not working

Discussion in 'Input System' started by Liam_GameDev, Aug 4, 2023.

  1. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    the Input for jumping will not work and when the game is started two errors appear that say:
    NullReferenceException: Object reference not set to an instance of an object
    playerMovement.OnDisable () (at Assets/Scripts/playerMovement.cs:38) before it lands on the ground.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class playerMovement : MonoBehaviour
    7. {
    8.     public Inputmaster controls;
    9.     //public float jumpPower;
    10.     public Rigidbody2D rb;
    11.     // Start is called before the first frame update
    12.     void Awake()
    13.     {
    14.         controls.player.jump.performed += ctx => Jump();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.      
    21.     }
    22.  
    23.     void Jump()
    24.     {
    25.         Debug.Log("boing");
    26.     }
    27.  
    28.      
    29.  
    30.  
    31.     private void OnEnable()
    32.     {
    33.         controls.Enable();
    34.     }
    35.  
    36.     private void OnDisable()
    37.     {
    38.         controls.Disable();
    39.     }
    40. }
    41.  
     
    Last edited: Aug 4, 2023
  2. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    171
    Hi there,
    What is "Inputmaster" class? Could you share?

     
  3. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    887
    Looks like the new input system and doesnt look like you actually initialised the class
     
  4. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    Its a input action
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,198
    You need to initialise the class. Ergo,
    public Inputmaster controls = new Inputmaster();


    Normal C# stuff.

    You will also need to
    .Enable()
    the input action map as well.
     
  6. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    I don't really understand that last part
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,198
    I realise you were doing it further below in your script, but basically:

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.     public InputMaster controls = new InputMaster();
    4.    
    5.     void Awake()
    6.     {
    7.         controls.Enable();
    8.         controls.player.jump.performed += ctx => Jump();
    9.     }
    10. }
    Example code. Not to be taken as is.
     
  8. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    even after making the controls variable a new input action it still dose noy work and I am getting four errors two that say NullReferenceException: Object reference not set to an instance of an object and two that say UnityException: CreateScriptableObjectInstanceFromType is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'playerMovement'. these error happened in the window of when I start the game and my player making contact with the ground
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,198
    Inputmaster is a scriptable object??? I've no idea how you've done that.

    You're meant to generate a C# class out of your input action asset and use that:
    upload_2023-8-7_19-11-32.png

    In this case it just sounds like you haven't assigned it via the inspector.
     
  10. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    887
    So show your current code - we cant see from here
     
  11. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    I already have generated a C# class

    this is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class playerMovement : MonoBehaviour
    7. {
    8.     public Inputmaster controls = new Inputmaster();
    9.     //public float jumpPower;
    10.     public Rigidbody2D rb;
    11.     // Start is called before the first frame update
    12.     void Awake()
    13.     {
    14.        
    15.         controls.player.jump.performed += ctx => Jump();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.        
    22.     }
    23.  
    24.     void Jump()
    25.     {
    26.         Debug.Log("boing");
    27.     }
    28.    
    29.        
    30.    
    31.  
    32.     private void OnEnable()
    33.     {
    34.         controls.Enable();
    35.     }
    36.  
    37.     private void OnDisable()
    38.     {
    39.         controls.Disable();
    40.     }
    41. }
    42.  
     
  12. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    887
    Exactlly where is it now saying the error is? because you have moved everything since you last shared
     
  13. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    the errors are UnityException: CreateScriptableObjectInstanceFromType is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'playerMovement'. and NullReferenceException: Object reference not set to an instance of an object
     
  14. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    887
    Every error message tells you exactly what the problem is, So its told you you cant initialise it like that. So what did you try?
     
  15. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    I have been able to narrow it down to one error it has a problem with line 14 it says NullReferenceException: Object reference not set to an instance of an object I can't figure out how to fix it
     
  16. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    887
    well line 14 was empty last we saw .. keep working on it
     
  17. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    the code that was on line 15 is now on line 14
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class playerMovement : MonoBehaviour
    7. {
    8.     private Inputmaster controls;
    9.     //public float jumpPower;
    10.     public Rigidbody2D rb;
    11.     // Start is called before the first frame update
    12.     void Awake()
    13.     {
    14.         controls.player.jump.performed += ctx => Jump();
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        
    21.     }
    22.  
    23.     void Jump()
    24.     {
    25.         Debug.Log("boing");
    26.     }
    27.  
    28.     private void OnEnable()
    29.     {
    30.         controls.Enable();
    31.     }
    32.  
    33.  
    34. }
    35.  
     
  18. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    887
    You still have not initialised it.. (please bang your head on the desk repeatedly)
     
  19. Liam_GameDev

    Liam_GameDev

    Joined:
    Feb 5, 2023
    Posts:
    26
    I have got it working thanks for your help