Well, I know this has been asked already and I also happen to know the easiest way to do so.
Now my question is more about your advice on if there is a better way.
I want my method to be called only once when the component is enabled or created. See I can create a component but keep it disabled, then when I enable it for the first time, I want the Init method to be called. The component is contained into an "attached" object.
So I have the Component with
internal bool _runOnce;
then I have the MainObject
List<Component> _listComp = new List<Component>();
void Update(){
foreach(Component c in _listComp){
if(!c.enable)continue;
if(c._runOnce){
c.Init();
c._runOnce = false;
}
c.Update();
}
}
My main concern is that the check for _runOnce will happen every frame for every component on each object. I know it is just a boolean check and is worth nothing but I am just asking if anyone would know a pattern for this purpose that is better than this.
Thanks