Why is OnRenderObject() called after OnPostRender()??

There must be a reason for this but it seems very counterintuitive. After a while investigating this the debug output clearly shows that a Camera’s OnPostRender() is called before OnRenderObject() with that camera. From several tests this seems to be the behaviour.

Camera1 calls OnPreCull() etc

Camera1 calls OnPostRender()

Object1 OnRenderObject() gets called some time later with Camera1 as current camera…

The documentation is not clear on this either stating:

OnRenderObject: Called after all regular scene rendering is done.

OnPostRender: Called after a camera finishes rendering the scene.

It is unclear what being called “after other rendering is done” and “after the camera finishes” means in this context.

I dont know if this is a bug, bad documentation or just bad design (or i’ve potentially deeply misunderstood something) but I can someone please help me find a function such as OnPostRender() on a Camera that is actually called after it has finished everything? (including custom meshes and procedural objects). My camera puts some specific objects onto a layer before rendering (so only they are rendered by the camera) and is supposed to put the objects layer back to default in OnPostRender() AFTER rendering them, however the above ‘quirk’ means this doesnt apply to Graphics.DrawProcedural objects etc. as their layers are reset before they get to be rendered

If anyone has any (not too hacky) workarounds for this they would be much appreciated.

Thanks

OnRenderObject and OnPostRender are (from what I understand) more or less the same thing, except OnRenderObject can go on any GameObject, while OnPostRender has to go on a GameObject with a Camera component.

The way I read that is that you can’t rely on them having a particular order. I definitely agree it’s counterintuitive.

An easy workaround is to use WiatForEndOfFrame() - put whatever you want to do in a method that you call like this:

void OnEnable() {
    StartCoroutine(WaitRoutine());
}

void OnDisable() {
    StopAllCoroutines();
    //or use a reference to it if you want others to keep going or whatever
}

IEnumerator WaitRoutine() {
    while(true) {
        yield return new WaitForEndOfFrame();
        DrawLast();
    }
}

void DrawLast() {
    //do your drawing in here - it will happen *after* all OnPostRender and OnRenderObject executions :)
}