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
  3. Dismiss Notice

im trying to get my joystick input to work but there is a error in the code

Discussion in 'Scripting' started by PhatCatDev11, Aug 2, 2021.

  1. PhatCatDev11

    PhatCatDev11

    Joined:
    Apr 10, 2021
    Posts:
    16
    im getting a error saying:
    NullReferenceException: Object reference not set to an instance of an object
    JNMove.Awake () (at Assets/JNMove.cs:37)

    here is the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class JNMove : MonoBehaviour
    7. {
    8.     public Animator antr;
    9.  
    10.     Vector2 move;
    11.  
    12. PControls P;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.        
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (move.x > 0)
    24.         {
    25.             antr.SetBool("IsWalking", true);
    26.         }
    27.         else if (move.x < 0)
    28.         {
    29. antr.SetBool("IsWalking", true);
    30.         }
    31.    
    32.       Vector2 m = new Vector2(move.x * 5, 0) * Time.deltaTime;
    33. transform.Translate(m, Space.World);
    34.     }
    35.     public void Awake()
    36.     {
    37. P.GP.MoveHoz.canceled += ctx => move = Vector2.zero;
    38.  
    39.     P = new PControls();
    40.  
    41. P.GP.MoveHoz.performed += ctx => move = ctx.ReadValue<Vector2>();
    42.     }
    43. void OnTriggerEnter2D(Collider2D col)
    44. {
    45. if (col.gameObject.CompareTag("Map"))
    46. {
    47. antr.SetBool("IsSpacePressed", false);
    48. }
    49. if (col.gameObject.CompareTag("Pushable"))
    50. {
    51.     antr.SetBool("IsSpacePressed", false);
    52. }
    53. }
    54.  
    55. }
    56.  
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    It's a NullReferenceException Error, which means you're trying to access an object which hasn't been assigned.
     
  3. PhatCatDev11

    PhatCatDev11

    Joined:
    Apr 10, 2021
    Posts:
    16
    it's at line 37 it doesn't tell me what part is accessing a object, im trying to reference a input action map
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Have you tried debugging it? You can use Debug.Log(string) to print out objects, and if it's null you know that's the part which it can't access.
     
  5. PhatCatDev11

    PhatCatDev11

    Joined:
    Apr 10, 2021
    Posts:
    16
    That's a good idea I'm going to try it
     
  6. PhatCatDev11

    PhatCatDev11

    Joined:
    Apr 10, 2021
    Posts:
    16
    I debug.logged the pcontroller p and it said null
     
  7. PhatCatDev11

    PhatCatDev11

    Joined:
    Apr 10, 2021
    Posts:
    16
    and i also found out that the code shown here is causing the problem in the script shown earlier at line 37 around the P.GP.MoveHoz.canceled part
     
  8. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    Perhaps it is because you are not defining a value for P until the next line:
    Code (csharp):
    1. P = new PControls();
    which is after you have tried to do the
    P.GP.MoveHoz.canceled
    part.
    What happens if you move line 39 to before line 37?
     
  9. PhatCatDev11

    PhatCatDev11

    Joined:
    Apr 10, 2021
    Posts:
    16
    Omg thank you so much it is working!!! Thx
    2 u all 4 helping me!!!!