The Reset() method in MonoBehaviour is invoked when you right click on a script attached to a GameObject or prefab and click “Reset” or when you first attach your script to a GameObject or prefab in the inspector.
All objects within Mono derive from Object. When you cast any object to a valid interface, it will be assumed to derive from Object as the very root of the object inheritance hierarchy. It cannot ever be anything else.
I think you are confused between what interfaces are for and how they are implemented by the developer.
I still believe that declaring an interface with a Reset() method in it, or any of the other reserved functions that MonoBehaviours have such as Awake, Start, Update, OnGUI, and so on and so forth, is a huge mistake and will lead to bugs further down the road.
The only valid reason to have an interface with one of the MonoBehaviour reserved functions is to ensure that when you need to invoke a method from another class, e.g. the EnemyMonitor class in my example, that the method has actually been declared because you test if the MonoBehaviour “is a” interface, and can then cast the to the interface to invoke the method without throwing a run-time exception without having to resort to reflection to verify that the function exists in the first place. Of course, Unity3D provides for this functionality built-in via the SendMessage method, but the interface makes the method declaration explicit whereas the reflection via SendMessage makes the method declaration implicit.
Let us assume that we wish to invoke the method Update() on a MonoBehaviour from another script, for whatever particular reason we may wish to do this:
The incorrect way:
public class Enemy01Behaviour : MonoBehaviour
{
public void Reset()
{
// reset our resetable stuff
}
}
public class Enemy02Behaviour : MonoBehaviour
{
// we don't declare a reset function in here
}
public class EnemyMonitor : MonoBehaviour
{
void ProcessAI()
{
MonoBehaviour[] enemyIntelligence = GameObject.FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[];
foreach (MonoBehaviour ai in enemyIntelligence)
{
// this won't even compile because we are trying to invoke a method that MonoBehaviour does not declare
ai.Reset();
}
}
}
The interface way:
public interface EnemyAI
{
void Reset();
}
public class Enemy01Behaviour : MonoBehaviour, EnemyAI
{
public void Reset()
{
// reset our resetable stuff
}
}
public class Enemy02Behaviour : MonoBehaviour, EnemyAI
{
// this won't compile because we inherit from EnemyAI interface but do not implement the Reset function
// we don't declare a reset function in here
}
public class EnemyMonitor : MonoBehaviour
{
void ProcessAI()
{
EnemyAI[] enemyIntelligence = GameObject.FindObjectsOfType(typeof(MonoBehaviour)) as EnemyAI[];
foreach (EnemyAI ai in enemyIntelligence)
{
ai.Reset();
}
}
}
The Unity3D way:
public class Enemy01Behaviour : MonoBehaviour
{
public void Reset()
{
// reset our resetable stuff
// this function will be invoked both by our code and also by the Unity3D inspector/editor
}
}
public class Enemy02Behaviour : MonoBehaviour
{
// we don't declare a reset function in here
}
public class EnemyMonitor : MonoBehaviour
{
void ProcessAI()
{
MonoBehaviour[] enemyIntelligence = GameObject.FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[];
foreach (MonoBehaviour ai in enemyIntelligence)
{
// this will throw a run-time exception because one our MonoBehaviours does not have a Reset() method
// we can fix it by appending using the DontRequireReceiver enum
ai.SendMessage("Reset");
}
}
}