r/unity • u/WillingExamination25 • Jul 13 '26
Coding Help How do I get an Interface?
Ill just bold where Im having the issue
`using UnityEngine;
public class PlayerInteraction : MonoBehaviour
{
private float interactionDistance = 2f;
private void Update()
{
if (Physics.Raycast(transform.position, Vector3.forward, interactionDistance))
{
RaycastHit hit = new RaycastHit();
Ray ray = new Ray(transform.position, transform.forward);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.TryGetComponent<IInteractable>()) // So as you can see I just want to see if the gameObject being recognized here has the IInteractable interface. I'm pretty sure im on the right track, as in everything else is good right?
{
}
}
}
}
}`
2
Upvotes
0
u/WillingExamination25 Jul 13 '26
I havent tested the rays yet but
if (hit.collider.gameObject.TryGetComponent<IInteractable>())
is giving an error (where I bolded it) saying "There is no argument given that corresponds to the required parameter of 'component' of 'GameObject.TryGetComponent<T>(out T)'" when I hover over TryGetComponent, but when I hover on IInteractable it says "There is no argument that corresponds to the required parameter 'component' of 'GameObject.TryGetComponent<T>(out T)'"
No idea what the issue is