- Home /
Is OnEnable fired right after Awake?
Scenario:
I have a View that's supposed to subscribe to an event from a controller to update a Text.text with a score only if the score in the controller gets updated.
For this I make an event in my controller (called BattleInstance) and make my controller a singleton in Awake():
public event System.Action<int> UpdatedPoints;
private void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != this)
{
Destroy(gameObject);
}
}
In my BattleViewController I want to make sure I subscribe and unsubscribe (Bye,Bye memleak ;) ) in the OnEnable() and OnDisable()
private void OnEnable()
{
BattleInstance.instance.UpdatedPoints += this.UpdatePointDisplay;
}
private void OnDisable()
{
BattleInstance.instance.UpdatedPoints -= this.UpdatePointDisplay;
}
Now when I run this I get this error
NullReferenceException: Object reference not set to an instance of an object BattleViewController.OnEnable () (at Assets/Scripts/BattleScene/ViewControllers/BattleViewController.cs:44)
when trying to subscribe because apparently the OnEnable in my ViewController is called before the Awake in my Controller. :(
Subsequently of course, when my Controller fires the event in the Start() to initialize the score display as 0 I get:
NullReferenceException: Object reference not set to an instance of an object BattleInstance.Start () (at Assets/Scripts/Administration/BattleInstance.cs:38)
EDIT:
To specify my question(s):
Do I understand it correctly that the OnEnable is fired right after an object's Awake? So the order being Object1.Awake() | Object1.OnEnable() | Object2.Awake() | Object2.OnEnabnle() | Object1.Start() | Object2.Start ?
How do I in my Object1.OnEnable() make sure that Object2 is Awake?
Answer by Ashokkumar-M · Aug 10, 2017 at 10:45 AM
Hi, @quicksly
Yes OnEnable() is called after Awake() Unity Function Execution Order
But you can change it by using the following,
Thanks. This really helps a lot.
So if I understand it correctly in my case moving the BattleInstance controller script in the Execution Order to the top of Default Time will make sure that it gets the Awake() first and is ready for anything needing it's singlton, right?
Yes you should do it like that and if you need you can add more scripts and rearrange it. @quicksly
Answer by ShadyProductions · Aug 09, 2017 at 04:39 PM
The event is only fired the moment an item is enabled.
Apparently that is right after the Awake of that same object and not after all the Awakes are finished, right?
So how can I make sure that my Controller Instance is Awake before my View is enabled?
Your answer
![](https://koobas.hobune.stream/wayback/20220612132340im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to initialise another 'game mode' on click? 1 Answer
Dictionary in second script empty 1 Answer