#Can I customize the Initialization of a monobehaviour like I can change the Player Loop?

1 messages · Page 1 of 1 (latest)

azure anchor
#

So, I found this

https://docs.unity3d.com/Manual/player-loop-customizing.html

And that shows how you can add your "Fancy cool update method" to the player loop.

But what i need is a LateAwake, an awake that happens after all awakes have happened (Like LateUpdate but for awake)

Right now, I am using Awake and OnEnable but OnEnable can be called multiple times while Awake can only be called once.

I could use a flag, so OnEnable wouldn't execute more than once, but maybe there is a better way 🤔

plucky lark
#

… Start?

azure anchor
#

Start and OnEnable get called again if the component was disabled and re-enabled.
I can gate the execution behind a boolean, but maybe there was a better way 😅

plucky lark
#

Docs say otherwise

#

“Start is called exactly once in the lifetime of the script and always after MonoBehaviour.Awake”

azure anchor
#

Ohhhh, maybe I missread the flowchart? 🙃

plucky lark
#

Yeah flowchart is being a little liar while trying to convey flow and order i think

#

Oh wait no

#

the black arrow and grey arrow are meant to convey different flows

#

So off the grey onenable is a dead end

azure anchor
#

OnEnable definitely gets called again if I disable and re-enable a component 🤔

plucky lark
#

Yes

azure anchor
#

let me do a quick test about start 😐

#

Indeed on start is not called twice, only OnEnable

I've been played by the docs 🥲

#

(It would still be cool to add my own method inbetween, but hey, it works 🙏 )

floral brook
plucky lark
#

Don’t think thats what they mean

floral brook
#

Might be, or not 😄 They were talking about calling one awake after ALL the others have been called. Thats execution order for me. But if they want MULTIPLE awakes to be called after all the subtractive others have been called, you are right

azure anchor
#

The script execution order is cool, but it sorts between methods
So, it sorts all the Awake calls, all the Start calls, etc.
Just like you can never have a Start happen before an Awake, I am trying to get a LateAwake that can never happen before Awake and never happen after Start 😅

frosty anvil
#

tbf the flowchart does specify "Start is only ever called once"

true siren
#

LateAwake doesn't really make sense because Awake doesn't happen at a fixed time, it's just whenever a unity object becomes active (it works for non-MonoBehaviour objects too), that just necessarily means it gets called before Start

#

usually Awake calls happen because you did something like load a scene or instantiate a prefab, so you should be able to find the point in your code after that where whatever "LateAwake" behaviour you need can be inserted

plucky lark
#

LateAwake makes sense

#

It’s literally start lol

true siren
#

they're not the same at all? Start is related to the Update loop, they might happen at totally different times

plucky lark
#

Awake happens once
Start happens once, after awake

true siren
#

Start happens the first time that object would get an Update, not immediately after Awake

#

they're different events for a reason! the difference can be important

#

you can implement your own "LateAwake", you just need to define what that actually means, like what is it later than? if you just want it to run after every other script in a prefab or scene wakes up, you can do something like this (or whatever more sophisticated equivalent to BroadcastMessage you prefer)

var obj = Instantiate(prefab);
obj.BroadcastMessage("LateAwake");
burnt palm
#

Id use an interface instead of messages. Its not hard to perform your own object initialization

floral brook
true siren
#

one example where you might get Awake and not Start is spawning an object and then deactivating it, or using AddComponent and then setting enabled = false, in which case Start won't be called until the object gets reactivated/reenabled later or might never be called

#

they always happen in the same order, but you can't assume Start will always be called after an Awake call

floral brook
#

Thats another statement, but true, I agree. I am just assuming, this is not whats intended here, as the person wants to get whatever method after the awake. So they might not run into this situation on this specific topic