When you unload a scene, any object in that scene that was not marked DontDestroyOnLoad()
(or not parented under a root object marked this way) will be destroyed. It doesn't matter whether another object still holds a reference to it. If you didn't tell Unity you wanted it exempted from the scene unload, then it will be destroyed just like everything else in the scene. That leaves a hole where it used to be - the "Missing (Game Object)" links you're observing.
The reason that you seem to still see the object in your scene is because you're reloading the same scene. So that spawns new copies of the leaderboard, etc. But the leaderboard that exists in the current scene is not the one your object was referencing.
If you have a cluster of objects that all depend on one another, then if you mark one as DontDestroyOnLoad
, you'll probably want to mark all of them that way, to ensure they get preserved together. One simple way to do this is to put them under one root object.
- Persistent Objects (Marked
DontDestroyOnLoad()
, contains your script referencing the others)
By marking the root object as not to be destroyed, the other objects under it won't be destroyed either.
Using DontDestroyOnLoad
this way gets messy though, because when you re-load the same scene, you'll still get new copies of these objects, in addition to the surviving object that evaded destruction when the scene was unloaded. You can fix this by deleting the copies on scene load, or by moving these long-lived objects to their own scene that you load additively, and never unload/reload, so you never have to fuss with DontDestroyOnLoad
in the first place.
DontDestroyOnLoad(leaderboardPanel)
or on the canvas that contains it? \$\endgroup\$