This is a pretty esoteric issue, but I was hoping someone can help me out here. I have a few scripts to which I’ve added the ExecuteInEditMode attribute in order to use their funcitonality to manipulate some “seed” objects that are used in-game. These scripts dump a bunch of objects into the scene as part of their function. Until this point, the objects were created in “Play” mode and thus destroyed when stopping the game, as with any in-game change.
However, now that the script has the ExecuteInEditMode tag, those objects stick around when the game is stopped, and then stack up with each subsequent run of the game. Thankfully, the objects are more-or-less “passive” in that they don’t actually make much of a difference on the game itself if they’re duplicated.
However, I’d really like to stop this from happening, since it clutters the scene, and I imagine that it will eventually adversely affect performance to have thousands of useless gameobjects sitting around.
I tried destroying them in the script’s OnDestroy or OnApplicationQuit callback, but while these functions fire at the appropriate time, they don’t actually delete the object, even if I use GameObject.Find() as opposed to the variable reference to the gameobject.
Any ideas?
Edit: To be clear, all I really need is somewhere to inject some code that will happen after the “context switch” between game and editor, on the editor-side so that I can remove from that set of objects.
Sorry for the belated response. Was on vacation There are a couple of different paths by which I create the object. In the case that they build up, they’re being created in the Start() of an object in the scene. They’re all added as the child of an object created by a public static accessor. Here’s what I mean.
Parent object:
Here’s a particularly odd wrinkle. I tried calling ClearContainer() right before a set of calls that then accesses the container (to add sub-objects). Everything works fine, and if I Debug.Log the value of MiscContainer, it returns an object, but it’s not visible in the scene! WTF?
Edit: I should clarify, not “everything works,” but the code doesn’t error. In-game, I get a fatal crash (have to reboot Unity) so clearly something isn’t acting as it should.
it seems that start is called multiple times when going in and out of play mode, more so, the CONSTRUCTOR is called multiple times. I know this because i have a
public List connections = null;
and this becomes null everytime i go in and out of play mode.
Haven’t quite figured out how to get around this, I might have to create a game object and stuff each node’s connections into a master list. This will allow me to check for the gameobject before adding any connections. Still, I feel the editor is just flat out broken in this sense.