- Home /
The question is answered, right answer was accepted
How to make sure custom init methods are called?
I'm having a little trouble with Unity's component approach. I've created an Dad class that extends MonoBehaviour and a Family class that also extends MonoBehaviour but also has a reference to Dad.
I can make sure that Family has always a Dad reference by using [RequireComponent], the problem is that every Dad should have a parameter 'name', and this parameter must be set by it's Family.
As I shouldn't use constructors on MonoBehaviours I've created a method to set the name:
public void Init(string aName) {
this.name = aName;
}
But I want to receive an error when I forgot to call Init. Is there a way to make sure the Init method will always be called?
Shouldn't it actually be the other way around, that a Dad always has to have a Family? Then every Dad will always get a name.
Anyway, I think what you want to do here is make sure that Dad.name is always set when you want to use it. For that purpose you can have a getter function or property which checks that:
public class Dad : $$anonymous$$onobehaviour {
private string name;
public string Name {
get {
if (string.IsNullOrEmpty(name)) {
Debug.LogError("Dad " + gameObject.name + " does not have a name!");
}
return name;
}
}
...
}
Answer by xarismax · Nov 09, 2017 at 06:37 PM
At Family.Start() -> call Dad.Init(name);
Think as Dad as a "Data Object", Family is responsible for calling its methods. For this reason don't add Awake or Start on Dad, only Family class is allowed to initialise Dad.
1) Initialization Responsibility: This makes explicit that Dad is not responsible for initializing itself, the responsibility falls to another class (Family).
2) Explicit Execution Order: This ensures that Family will be called before Dad, as Dad is initialized from Family before used.
Yeah, that's what I was going for, but what if when I create a different class Another$$anonymous$$indOfFamily that also uses Dad? Every time I use the Dad component in another class I would have to remember to call Init.
That's what I was referring to: either you remember to call it, or you reverse the dependency. For example you could create a Family interface/abstract base class FamilyABC, and in Dad.Start()
do FamilyABC family = GetComponent<FamilyABC>(); if (family != null) { family.Init(this); }
.