Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Objects being destroyed and scene is empty on Play

Discussion in 'Editor & General Support' started by Jiroxys, Aug 27, 2023.

  1. Jiroxys

    Jiroxys

    Joined:
    Mar 1, 2021
    Posts:
    2
    I have run into an issue that has left me utterly stumped for three days. I cannot for the life of me figure out why my GameObjects are being destroyed (there are no Destroy() calls anywhere in my code yet.) and why my scene is suddenly being completely cleared out when I hit run.

    I need to emphasize again, I do not have any Destroy() calls anywhere in my project. My project is still in its infancy and I am 100% certain that there are no such calls hiding anywhere that I have written.

    I have tried stepping through my code from start to finish multiple times, and it seems almost as if Unity is destroying my game objects on the back end because they'll just go from existing to being null inexplicably between steps.

    I have even tried the void OnDestroy(){ LogError(); } trick that is supposed to allow you to see what called it, but the only thing that ever appears in the call stack is this OnDestroy() method with no mention of where the call came from.

    I'm at an absolute loss and I cannot get anything back to normal. Any ideas on where to go from here or what on earth is going on would be greatly appreciated.

    Pics:
    The scene as it appears in edit mode. The big "Button" is a UI element that disappears as well.
    upload_2023-8-27_13-37-4.png

    Game view once Play is pressed:
    upload_2023-8-27_13-38-0.png

    Scene while the game is playing. Double-clicking anything in the hierarchy snaps back to this location but there is nothing there:
    upload_2023-8-27_13-38-20.png

    The call stack showing no helpful information when OnDestroy() is being called from the somewhere in the aether:
    upload_2023-8-27_13-39-28.png
     

    Attached Files:

  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Did you check DestroyImmediate too? Editor scripts may cause this. Or loading another scene.
    Duplicate the scene (ctrl+d). Is the behaviour the same?
    Create a new scene. Add objects that you need one by one. Does the destroy behaviour come back? If so, check the most recently added objects and their scripts, including editor scripts.
     
  3. Jiroxys

    Jiroxys

    Joined:
    Mar 1, 2021
    Posts:
    2
    Thank you so much for the debugging advice! I was able to pinpoint the problem by recreating my scene.

    I have to admit I was not being honest about there being no OnDestroy() calls. I had a few singletons that contained the only instances of the call within them. While I did confirm that the Destroy calls were only destroying the singleton instances, I had somehow managed to put a singleton script onto my Canvas as a component. So when I added the intentional singleton as its own GameObject in the scene, this created a duplicate which resulted in said Canvas being destroyed at runtime. After removing the component from the Canvas, everything was back in working order.