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 instantiating new children at runtime creates duplicates every time i stop the game

Discussion in 'Scripting' started by canslp, Aug 22, 2023.

  1. canslp

    canslp

    Joined:
    May 6, 2019
    Posts:
    36
    so i'm using Instantiate() to add some prefab clones into the scene, as children to my root object. it seems like when you do this, the children are not destroyed when you stop the game. this is probably because i'm instantiating things on Start() in an [ExecuteAlways] monobehavior. because of this, i'm also taking care to delete all of the children on Start(), before i spawn the new ones.

    this is having a really odd effect where when i hit play, all the children are deleted and a new one is spawned (the expected behavior) but then when i stop the game, all of the old children are undeleted. the effect is that each time i hit play, there is one child in the scene, and them each time i hit stop the number of children becomes the number of times i've played the game- ie all of the children that have ever been spawned reappear with the new child.

    i think that this is because of the way unity tries to restore the state of the scene each time you stop the game, but for some reason this rollback doesn't include rolling back spawning the new prefab, so it only rolls back deleting the old children. any ideas?
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I knew about the call [ExecuteInEditMode], but never heard of that one, nice call out! I was actually thinking of how to have script make a bunch of objects for scene saving, the other day. Instead of duplicate a gazillion times, lol..

    I'm pretty sure there are
    Application
    calls, to where the script can see that the application has quit, and is in edit mode. So I would just have that set to destroy(if that's what you want) in the objects script. Maybe even a public bool on your main script that says all objects delete, so you can just click that if you're not happy with the results.
     
    canslp likes this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Have you checked whether Start() runs after exiting playmode?

    I’d say Start() also runs because exiting playmode also means you enter edit mode.
     
    canslp likes this.
  4. canslp

    canslp

    Joined:
    May 6, 2019
    Posts:
    36
    oh it probably definitely does. the question is why would than not delete all the old children, since that happens on start
     
  5. canslp

    canslp

    Joined:
    May 6, 2019
    Posts:
    36
    i mean really what i want to happen is for it not to un-delete all of the old objects. what it is supposed to do is re-generate all the objects that are meant to exist without duplicates. it honestly basically works in play mode, like the number of objects that exist when you hit play is correct. it's just really annoying that every time you leave play mode the scene is filled with an increasing number of duplicates