- Home /
The question is answered, right answer was accepted
How to replace the old "Awake"?
The old Awake was called when the script was loaded even if the the gameobject was disabled. The new Awake was called only when the gameobejct is active.
How to replace the old Awake() void with something else? I have to run a command at the start, on inactive gameobjects.
(the pages in my game menu are inactive at the start, but they have to find their components at the start. Other scripts can refer to the menu pages, even if they are inactive, so I have to run that command before the gameobject is enabled)
Answer by Bunny83 · Sep 18, 2021 at 06:36 PM
Awake was never called on disabled gameobjects. It is called in disabled scripts if the gameobject is active. However there is no initialization callback if the gameobject is inactive. There never was one.
So you want to rely on scripts that conceptionally do not participate on the game to do something for you. This is simply the wrong approach. If they need to be inactive, you have to trigger any initialization from outside. Either use lazy initialization, or provide your own initialization method that you call from another script.
That's all correct, of course, but it sounds like perhaps they don't actually need to be inactive on Awake, they just need to be inactive before rendering.
So another possible approach would be to have these menu pages active in the scene, so their Awake functions get called for set up, and only then make them inactive.
I use this kind of approach sometimes when I have a bunch of objects which are to be controlled by some kind of manager - their Awake functions register them with the manager, and then the manager's Start function can choose which if any of them should be initially active (and is subsequently able to de/reactivate them as required).
Follow this Question
Related Questions
Toggling a game object between an active and inactive state 1 Answer
Activate gameobject that is deactivated with c# 2 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Instantiate inactive object 4 Answers
If i disable a GameObject does all it's components get disabled also? 2 Answers