Search Unity

Dont Destroy on Load duplicating objects

Discussion in 'Editor & General Support' started by EX_34, Oct 6, 2019.

  1. EX_34

    EX_34

    Joined:
    Jan 31, 2016
    Posts:
    11
    So I came across a weird issue yesterday and I was wondering if anyone had information on this. I was able to fix the issue with a work around, but this is totally weird.

    I have an inventory object that exists in my main menu scene. Players are able to take items from their inventory and add it to the ships inventory in order to edit their ships capabilities. The inventory system in the scene which holds the ships items gets duplicated with dont destroy on load when I start the scene and I have to delete the other one using a script I created. This happens on a single load of the scene. I am not leaving the scene and coming back into the scene and having another object instanced, it duplicates on a single load of the scene. I am not instancing this object, if I remove or deactivate the inventory, it doesnt get put into dont destory on load and it doesnt get dupliated.

    Whats even weirder is this seems to cause my start function to run more than once. I put a print statement into my script that destroys the duplicate object to tell me how many of the objects have been spawned. The scene starts, it prints once and says "Number of inventories 1" then a moment later it runs a second time and says "number of inventories 2" and deletes the extra object. I assumed I accidentally added this script to two objects, so I tried searching for the other object with the script on it, but nope, its not there. If I remove the single instance of the script it never runs at all, but if one is in the scene it runs twice.

    Has anyone had any experience with dont destroy on load duplicating objects?

    Examples bellow:
    This is my hiarchy before I hit play in my scene. I have disables my destroyer script which prevents the duplicate
    Before I hit play.png

    This is my scene after I hit play with the destroyer script turned off. There are clearly two inventory 2 tests now, but neither are instantiated since they dont say (clone)
    After I hit play.png

    This is my scene after I hit play if I leave the destroy script on. the destory script just runs in start, and you can see its printed multiple times to the console even though the start function should only run a single time.

    It prints the name of the object its on since I thought i added two of them into the scene, but this proved to me that it is a single script that has its start function called twice. But I am able to delete the duplicate object at least.
    After play with destroy script.png