Post process overlay with occlusion. Issue with flickering image.

I am trying to create a “Post Process Material” that will allow object to have a coloured overlay.
When the object is behind geometry or another object, it will no longer draw this overlay.
It achieves this by checking if the “SceneDepth” is shorter or longer than the “CustomDepth”.

It sort of works, however it keeps flickering wildly from the coloured overlay to the normal texture.
The part of making an overlay works fine on its own.
But when checking for depth something seems to go wrong.

Could anybody tell me how to fix this?

I tried a different method of occlusion.
This time copied directly from the official documentation.
The occlusion part works.
However it still gives this flickering image.

I made a new project on a different computer, with unfortunately the same result.
So I am guessing it has something to do with settings somewhere in the project, and not the nodes in the Post Processing Material.

Does anyone have a clue what it might be?
Maybe just a hunch, that is fine, I will try it.