So from what I can tell GetComponent works on interfaces but trygetcomponent has yet to give me the result I desire.
So this code supposedly checks if the script has the IOverworldInteractable interface. It makes me wonder though, if it can’t read an interface why isn’t it returning an error?
According to the debugs I reach step 2 so it’s not like interactable.Interact(); is dying on me.
In the case I cannot use TryGetComponent on interfaces, is there an alternative?
if (Input.GetKeyDown(interactKey))
{
Debug.Log("step 1");
ray.DoCast();
if(ray.GetRaycastObject())
{
Debug.Log("step 2");
if(TryGetComponent(out IOverworldInteractable interactable))
{
Debug.Log("step 3");
interactable.Interact();
}
}
//TODO:
//Make it so the current interactable closes and open when pressed E
}
In case it helps here’s an Interaction script:
public class OverworldNpc : MonoBehaviour, IOverworldInteractable
{
[SerializeField] new string name;
[SerializeField] private CharacterInteraction interactionText = new CharacterInteraction();
private void Awake()
{
if(interactionText.HasText())
{
interactionText.textBox = GameObject.Find("TextInteraction").GetComponent<TextBox>();
}
}
public int option
{
get;
set;
}
public void Interact(Party party = null)
{
string message = name + "\n\n" + interactionText.GetText();
interactionText.textBox.EnableText();
interactionText.textBox.DisplayText(message);
}
public bool ConditionsMet(Party party = null)
{
if(interactionText.HasText())
{
return true;
}
return false;
}
}