0
\$\begingroup\$

I have an empty object which is not destroy after the game is restarted.

DontDestroyOnLoad(this.gameObject);

I have some gameobject references in the script

public GameObject leaderboardPanel;
public GameObject addPointsPanel;
public GameObject updateButton;

When the scene is reloaded then all the above objects in inspector shows

Missing(GameObject)

enter image description here

after game is reloaded

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ Did you mean to flag those object as DontDestroyOnLoad too? \$\endgroup\$
    – DMGregory
    Mar 11, 2021 at 18:00
  • \$\begingroup\$ yes when game / scene is reloaded reference objects should also not get destory \$\endgroup\$
    – Ravi Pal
    Mar 11, 2021 at 18:06
  • \$\begingroup\$ So you called DontDestroyOnLoad(leaderboardPanel) or on the canvas that contains it? \$\endgroup\$
    – DMGregory
    Mar 11, 2021 at 18:07
  • \$\begingroup\$ leaderboardPanel is under canvas. DoNotDestroy is an Empty GameObject \$\endgroup\$
    – Ravi Pal
    Mar 11, 2021 at 18:11

1 Answer 1

1
\$\begingroup\$

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)
    • Canvas
      • Leaderboard Panel
      • etc...

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.

\$\endgroup\$
4
  • \$\begingroup\$ you mean Objects in canvas which are going Missing after reload should also be marked as DoNotDestory object via assigning a script to the panel \$\endgroup\$
    – Ravi Pal
    Mar 11, 2021 at 18:25
  • \$\begingroup\$ I'd recommend instead that you put the whole canvas under a parent that is DontDestroyOnLoad, or that you put it in a scene that you do not unload. \$\endgroup\$
    – DMGregory
    Mar 11, 2021 at 18:31
  • \$\begingroup\$ DontDestroyOnLoad only works for root GameObjects or components on root GameObjects. - When DontDestoryOnLoad applied to object under canvas. \$\endgroup\$
    – Ravi Pal
    Mar 11, 2021 at 18:48
  • \$\begingroup\$ Which is why, I repeat, I recommend that if you use DontDestroyOnLoad, you put everything you want to persist under one root object, and use it on that. \$\endgroup\$
    – DMGregory
    Mar 11, 2021 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .