- Home /
Is there a method on MonoBehaviour that is called on load even if GameObject is disabled?
I need to get a notification that script was loaded even if GameObject is disabled in hierarchy. I though Awake does that, but it isn't called until GameObject is activated.
Is there such method?
Hello,
I don't think there is any method for that, why would you need this exactly? there might be a way around your problem...
Answer by Hellium · Sep 21, 2015 at 02:17 PM
If you read the manual : http://docs.unity3d.com/Manual/ExecutionOrder.html
You will see that there is no method before Awake (except Reset) that is called if the GameObject is disabled.
A workaround would be : Enable the object in the hierarchy, and disable it in the Awake function. Thus, the start, Update, .... won't be called.
That's a terrible hack that reduces performance, increases memory load and bad program$$anonymous$$g.
Answer by amanpu · Sep 21, 2015 at 02:44 PM
@Hellium's answer is correct that there is no method which is invoked if gameobject is disabled in hierarchy , you need to disable gameobject in Awake().
Along with it you should also change Script Execution Order so that your this script is called before all other scripts and does not effecting execution of other script.
Answer by Xtro · Dec 03, 2016 at 10:29 PM
It's very strange that Unity team never needed to create this feature. It is a must for programming.
Anyways, I created this asset for this purpose!
I agree it is a must, it's essentially a constructor that is safe for unity.
It'S not really a must. You just have the wrong idea about gameobject usage. GameObjects that are deactivated are essentially not present for Unity. There should be no need for code to run on such gameobjects. The only reason would be that you access this object from the outside and use it from an active object. So you are violating this concept. If you want to work with a gameobject it should be active. If you need a specific behaviour of the gameobject you attach "behaviours" aka components to dictate its behaviour.
Don't get me wrong, sure a callback would be neat for some cases, however it's not really necessary. If you have a concrete issue that you can not solve without such a method, feel free to create your own question and we can give some recommendations how you can solve it. When you run into an issue with the world you should not expect the world to change but you should consider how you can adapt to the world. There is no perfectly designed API in this world. Complaining on a Q&A site about the current implementation does not bring you closer to a solution.
No it's not a must. You're quoting limitations of gameobjects, not of what is possible with program$$anonymous$$g, which is what the OP is asking. $$anonymous$$ove along if you're not able to provide an answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220612034513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
singleton becomes null? 0 Answers
Can't use Script Execution Order 1 Answer
Update and Awake not being called. 1 Answer
How to mock or skip execution of Awake in playmode unit tests? 2 Answers