Equivalent of awake() for all objects (inc. disabled)

I would like a method within game objects to be called even when it is disabled (in Unity).
For now I’ve managed to use the constructor() method but the object isn’t hydrated yet (with data from Unity). With a simple setTimeout(..., 0) it does the job, but I wondered if there was a better alternative, such as a ready() method to override in the same way as start() or awake().

Original Post on Discord

by user 615280976855171083

there exists one, one second

For my understanding: what are you planning to do with it?

You can add ISerializable to your component and implement e.g. onAfterDeserialize (this is called when a component is loaded from the gltf/glb and finished populating)

Hmm interesting. For my specific case it’s a simple script that stores in a TS class the instances of all objects that have that script (with an id set in Unity editor) to get them anywhere in code.
I’ve never liked the GameObject’s find methods because they all use the name or the structure.

by user 615280976855171083

There’s also an internal method which is used to reset state - something like “ready” would be maybe a good addition

Ok but for that you could use the constructor right?

For merely registering them?

because then onAfterDeserialize is not enough because you will miss any component you add at runtime (or if you instantiate anything)

Ahhh okay. Well the constructor isn’t enough, I must wait the next JavaScript’s tick.

by user 615280976855171083

id being a public property set in Unity editor.
image.png

by user 615280976855171083

Ok another option: you can patch the id field and register the object when the id is being written to

or even just make it a property

set(id:string) => { … } and store the actual id in an “_id” backing field


export class MyThing extends Behaviour {
    @serializable()
    set id(str: string) {
        this._id = str;
        // REGISTER ME
    }
    get id(): string {
        return this._id;
    }

    private _id!: string;
}

Hmmm that’s pretty clever. :eyes: Thanks for the advice ! :sparkles:

by user 615280976855171083

Works great ! :+1::sparkles:

by user 615280976855171083