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)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerInteract : MonoBehaviour
{
public class OnFootActions
{
public InputAction Interact { get; private set; }
// Other actions...
public OnFootActions()
{
Interact = new InputAction();
// Initialize other actions as needed.
}
}
public OnFootActions OnFoot { get; private set; }
private void Awake()
{
OnFoot = new OnFootActions();
}
private Camera cam;
[SerializeField]
private float distance = 3f;
[SerializeField]
private LayerMask mask;
private PlayerUI playerUI;
private InputManager inputManager;
public PlayerInput.OnFootActions onFoot;
// Start is called before the first frame update
void Start()
{
cam = GetComponent<PlayerLook>().cam;
playerUI = GetComponent<PlayerUI>();
inputManager = GetComponent<InputManager>();
}
// Update is called once per frame
void Update()
{
playerUI.UpdateText(string.Empty);
// create a ray at the center of the camera, shooting outwards
Ray ray = new Ray(cam.transform.position, cam.transform.forward);
Debug.DrawRay(ray.origin, ray.direction * distance);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, distance, mask))
{
Interactable interactable = hitInfo.collider.GetComponent<Interactable>();
if (interactable != null)
{
playerUI.UpdateText(interactable.promptMessage);
if (onFoot.Interact.triggered)
{
interactable.BaseInteract();
}
} else interactable = null;
}
}}
This is my code for the interactions, and below will be the interactable code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Interactable : MonoBehaviour
{
// message displayed to player when looking at an interactable obj
public string promptMessage;
public void BaseInteract()
{
Interact();
}
protected virtual void Interact()
{
//we wont have any code in this function
//this is just a template function to be overriden by our subclasses
}
}