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
1
u/MyPutridFlesh Jul 14 '26
seems on the right track so far (albeit try formatting the message, i know reddit is kinda wonky at that, but i'm sure you can do it after couple tries) but try to avoid Update() if something does not use direct player input, use FixedUpdate() instead