We can't use constructor in class derived from Monobehavior, so we define "Initialze" method to simulate constructor, defination like this:
public class Animal : Monobehavior
{
public void Initialize(string animalName)
{
// initialze animal
}
}
Then, there is is dog class, which has different initialize parameter list:
public class Dog : Animal
{
// Cannot override because different parameter list
public void Initialize(string animalName, Bone favoriteBone)
{
base.Initialize(animalName);
// initialze dog
}
}
Then, we create animal object like this:
var animalInstance = Instantiate(animalPrefab);
animalInstance.Initialize("animal name");
And we create dog object like this:
var dogInstance = Instantiate(dogPrefab);
dogInstance.Initialize("dog name", new Bone());
There is the problem, we can still call the "dogInstance.Initialize("dog name")" without Bone parameter, unlike C# constructor, if someday I'm drunk and write down the stupid code (or someone else come to write it), it's a big trouble.
So, is there a best way to initialize object derived from monobehavior?