So I’m trying to make an interface system for interactable items. Here’s the interface:
public interface IInteractable {
// Can be interacted with
public bool inter { get; set; }
// Interact function
public void Interact();
}
I took this and implemented it in the following test script, which I attached to a GameObject with proper colliders.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test_NPC : MonoBehaviour, IInteractable {
public bool inter { get; set; }
public void Interact() {
Debug.Log("Interact");
}
}
Then I took a 2D Box collider and connected it to the player and attached the following script to it:
private void OnTriggerEnter2D(Collider2D other) {
var interactable = other.gameObject.GetComponent<IInteractable>();
interactable.Interact();
}
It didn’t work, so I went in with a breakpoint, and- alas- interactable was returning null. other.gameObject was properly returning the NPC GameObject.
I’ve been struggling with this for the past few days, so I’ve been progressively simplifying the functions to no avail. Any help is greatly appreciated!