DontDestroyOnLoad....why duplicates only sometimes?

Have an app where I’ve had to persist a prefab across scenes.
So simply added a DontDestroyOnLoad to my Awake method.
However, was struggling with what I though was a complex bug in my coding which I traced to this prefab sometimes duplicating itself when a new Scene loaded.
In frustration I googled ‘DontDestroyOnLoad duplicate’ and discovered this is a fairly common problem.

So my question is, why does this happen randomly and not every time?
and
What is the best solution if you need a MonoBehaviour that persists across Scenes.

it’s not exactly random, it boils down to a fault in your game logic somewhere where you call instantiate to create a new instance of the prefab when the old one still exists…

best solution would be to resolve that logic flaw. Alternatively you could look at adding singleton type logic so when the duplicate tries to be created it deletes itself when it finds another copy.

It probably is that you already have that prefab on the scene, so when you load it you are already carrying the prefab from don’t destroy on load plus the new one that the scene is holding.

Edit: The solution is usually checking on awake if another instance of itself exist either by singleton or find object/component and if it does destroy one of them