Of course creating the instance automatically if it doesn't exist yet is also a perfectly valid option. So it can always be called from the Awake, OnEnable or Start functions of any other components, and it will just work. which grate on my nerves).Ĭlick to expand.OnEnable, which was used in the original code to assign the instance, will also only run for created and enabled objects, so I don't see any issue here.įindObjectOfType is more reliable than assigning the instance in OnEnable, because it works even if other classes try to get the instance before the OnEnable function has fired. I generally avoid this (the exceptions being Unity's built in singleton like input stuff. The only exception to this is if my singleton is scene specific rather than living the entire project. Thusly guaranteeing an initialized instance with its Awake/OnEnable called immediately (since CreateInstance calls these). but instead of FindObjectOfType, if it's null I create an instance. I create them in the moment rather than allowing them to exist in the scene. It's bit me in the butt many times before. but I personally don't like relying on undocumented behaviour. Which you could run tests for to determine the behaviour. And since the timing of this isn't documented, it could change in the future, or on the target build. So this value could be changed at any moment before its Awake is called. Note serialization/deserialization doesn't necessarily happen on the main thread either. That value may happen to be 0 as well, making this bug unnoticeable at first until you maybe change it in some scene randomly, since that 0 is just incidental. Depending on timing it may not have deserialized yet, in which case when OP sets 'playerLives' to 0, that might get reset to whatever the serialized value is. the state of the singleton could get overwritten when the singleton is initialized. So now my OnStartOrEnable is implemented with this mixin:Ĭlick to expand.Antistone's suggestion would/could work. That's because in my 3.0 library I've moved to a mixin system (I'm playing around with mixin ideas to save putting logic on SPComponent in cases its not needed). NOTE - you may notice if you go to those links that in my 2018 post my source code link doesn't have OnStartOrEnable. Thusly giving you a OnEnable like method that behaves more like Start. With this I call OnStartOrEnable either in 'Start' or in 'OnEnable' if and only if Start was already called. main reason I only did set this all up is because I personally run into it all the time. And on it I have a method called 'OnStartOrEnabled':Īnnoying part is that it requires setting this all up. I personally used a method where I created my own script called SPComponent. And isn't easily readable in code since it's all through the editor. So as a result your singleton isn't necessarily initialized (has OnEnable called) when MainMenuController has its OnEnable called.Īnnoying since if you need OnEnable you have to write a bunch of annoying logic.Īnnoying because setting up execution order is cumbersome. This happens when the instance of the MonoBehaviour is created, and just after Awake. OnEnable is called when the object is first enabled. Click to expand.So in regards to when a scene is first loaded.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |