I'm trying to make a door that you can interact with using RayCast and the Animator. Before starting the game everythings seems ok, but when I click start these 2 errors pop up:
GetComponent requires that the requested component 'PlayerInput' derives from MonoBehaviour or Component or is an interface. NullReferenceException: Object reference not set to an instance of an object PlayerInteract.Update () (at Assets/Scripts/Player/PlayerInteract.cs:37)
Here is the code for both scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInteract : MonoBehaviour
{
private Camera cam;
[SerializeField]
private float distance = 3f;
[SerializeField]
private LayerMask mask;
private PlayerUI playerUI;
private PlayerInput playerInput;
void Start()
{
cam = GetComponent<PlayerLook>().cam;
playerUI = GetComponent<PlayerUI>();
playerInput = GetComponent<PlayerInput>();
}
// Update is called once per frame
void Update()
{
playerUI.UpdateText(string.Empty);
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))
{
if(hitInfo.collider.GetComponent<Interactable>()!= null)
{
Interactable interactable = hitInfo.collider.GetComponent<Interactable>();
playerUI.UpdateText(interactable.promptMessage);
if (playerInput.OnFoot.Interact.triggered)
{
interactable.BaseInteract();
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInteract : MonoBehaviour
{
private Camera cam;
[SerializeField]
private float distance = 3f;
[SerializeField]
private LayerMask mask;
private PlayerUI playerUI;
private PlayerInput playerInput;
void Start()
{
cam = GetComponent<PlayerLook>().cam;
playerUI = GetComponent<PlayerUI>();
playerInput = GetComponent<PlayerInput>();
}
// Update is called once per frame
void Update()
{
playerUI.UpdateText(string.Empty);
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))
{
if(hitInfo.collider.GetComponent<Interactable>()!= null)
{
Interactable interactable = hitInfo.collider.GetComponent<Interactable>();
playerUI.UpdateText(interactable.promptMessage);
if (playerInput.OnFoot.Interact.triggered)
{
interactable.BaseInteract();
}
}
}
}
}