So I’m new with C# and unity and I thought it would be a good idea to take a deep and hard look at every single one of the scripting tutorials… and I’m kind of stuck on this one: Unity Connect.
From what I can understand, interfaces are kind of like “templates” that you can make to “force” your classes to have specific variables and/or functions… for example, I can have two classes, say class Enemy and class NPC, and I want both of them to be able to be killed, so I can create an interface to “force” both of them to have what is necessary for it, kind of like this:
public interface IKillable
{
public float life;
public void Die();
}
public class Enemy: Ikillable
{
public float life{ } //forced to put this
public void Die(){ } //forced to put this
}
public class NPC: Ikillable
{
public float life{ } //forced to put this
public void Die(){ } //forced to put this
}
Everything great thus far… BUT… what’s up with “IEnumerator” then? Keep in mind I understand perfectly how to use Coroutines and the yield statement but what I don’t understand is how an interface can be a return type for a function, which I assume is what happens when using coroutines. If having a function return an “IEnumerator” is possible, is it also possible for me to create a function somewhere that returns my “Ikillable” interface? if so… what would it be returning or what would it be its purpose? since my interface only has empty shells of variables and methods?
I know even the question itself is confusing as hell but I hope I was clear with what my doubt is… thanks in advance for any help with this!