Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question How do I fix this error in C#? (NullReferenceException)

Discussion in 'Input System' started by unknownunityuser1, Jan 12, 2024.

  1. unknownunityuser1

    unknownunityuser1

    Joined:
    Jan 12, 2024
    Posts:
    2
    Every time I look at the Keypad by a door, it spams me this error:

    NullReferenceException: Object reference not set to an instance of an object
    PlayerInteract.Update () (at Assets/Scripts/Player/PlayerInteract.cs:63)


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerInteract : MonoBehaviour
    7. {
    8.     public class OnFootActions
    9.     {
    10.         public InputAction Interact { get; private set; }
    11.         // Other actions...
    12.  
    13.         public OnFootActions()
    14.         {
    15.             Interact = new InputAction();
    16.             // Initialize other actions as needed.
    17.         }
    18.     }
    19.  
    20.     public OnFootActions OnFoot { get; private set; }
    21.  
    22.     private void Awake()
    23.     {
    24.         OnFoot = new OnFootActions();
    25.     }
    26.  
    27.     private Camera cam;
    28.     [SerializeField]
    29.     private float distance = 3f;
    30.     [SerializeField]
    31.     private LayerMask mask;
    32.     private PlayerUI playerUI;
    33.     private InputManager inputManager;
    34.     public PlayerInput.OnFootActions onFoot;
    35.  
    36.     // Start is called before the first frame update
    37.     void Start()
    38.     {
    39.         cam = GetComponent<PlayerLook>().cam;
    40.         playerUI = GetComponent<PlayerUI>();
    41.         inputManager = GetComponent<InputManager>();
    42.     }
    43.  
    44.     // Update is called once per frame
    45. void Update()
    46. {
    47.     playerUI.UpdateText(string.Empty);
    48.    
    49.     // create a ray at the center of the camera, shooting outwards
    50.     Ray ray = new Ray(cam.transform.position, cam.transform.forward);
    51.     Debug.DrawRay(ray.origin, ray.direction * distance);
    52.    
    53.     RaycastHit hitInfo;
    54.  
    55.     if (Physics.Raycast(ray, out hitInfo, distance, mask))
    56.     {
    57.         Interactable interactable = hitInfo.collider.GetComponent<Interactable>();
    58.  
    59.         if (interactable != null)
    60.         {
    61.             playerUI.UpdateText(interactable.promptMessage);
    62.  
    63.             if (onFoot.Interact.triggered)
    64.             {
    65.                 interactable.BaseInteract();  
    66.             }
    67.     }   else interactable = null;
    68. }
    69. }}
    70.  
    This is my code for the interactions, and below will be the interactable code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public abstract class Interactable : MonoBehaviour
    6. {
    7.     // message displayed to player when looking at an interactable obj
    8.     public string promptMessage;
    9.     public void BaseInteract()
    10.     {
    11.         Interact();
    12.     }
    13.     protected virtual void Interact()
    14.     {
    15.         //we wont have any code in this function
    16.         //this is just a template function to be overriden by our subclasses
    17.     }
    18. }
    19.  
     
  2. unknownunityuser1

    unknownunityuser1

    Joined:
    Jan 12, 2024
    Posts:
    2
    i resolved this now, heres how for anyone else:

    The problem is that I have both OnFoot and onFoot in my class, and they are causing confusion. The OnFoot property is of type OnFootActions, but you are trying to assign an instance of PlayerInput.OnFootActions to onFoot in the declaration. This leads to onFoot being a separate variable and not connected to my PlayerInteract class's OnFoot property.