- Home /
I created a "Madness" button that spawns 100 GameObjects (Zombies). However, I noticed that a sizable portion of them "vanish" not long after spawning. Am I reaching a hidden limit? Garbage Collector?
The title describes it all.
I essentially have a "Madness" button that i press which spawns a ton of enemies for me to kill (for testing). I notice that a bunch of them (as individual gameobjects) just vanish into thin air while the zombies are coming towards me. I can see the gameboject list shrinking a bit shortly after pressing my madness button.
So if my madness button spawns 100, approx only 50 actually survive.
Is this a garbage collector working? Am i hitting some kind of limit i don't know about? All my zombies have animations, hitboxes, and rigibody2ds. The only thing that can kill my zombies are colliders tagged with "Projectile". I'm not shooting my gun, so i know they aren't dying due to my code.
Then you have other code somewhere that is making them vanish. We can't find for you where that could be with this few information. Is this from a tutorial project or something else? maybe find the "Destroy" keyword in all your project to know what scripts are deleting gameObjects.
I$$anonymous$$O it has nothing to do with the garbage collector.
If you add GameObjects into the hierarchy, they will not be automatically garbage collected since even if you don't keep a reference to them in your code, the scene hierarchy does have one. Also 100 GameObjects is not "a lot" by any means. A 10x10 match 3 game would have a 100 game objects in the tiles alone, not to mention everything else that needs to be on the screen: hud, effects...
I would start by adding a Debug.Log to the part where you are destroying the zombies just to make sure that code doesn't get executed.
I will debug log that 1 line in my code where the zombies are destroyed. It shouldn't be called unless a "projectile" collider hit them... but worth trying. I'll report back.
Thanks for the info about GC --- i also thought that being referenced in the gameobject heirachy window should prevent them from being cleaned up.
Answer by Baste · Jul 08, 2015 at 09:51 AM
The garbage collector picks up C# objects that are not referenced anywhere anymore. This does not include game objects.
I can see two possibilities: - you've got some script destroying them - some kind of physics makes them fall through the world/be launched into space
Does the zombies actually dissapear? As in, are they still in the hireachy panel? If they are, check where they went, otherwise check what scripts you have in the scene when the zombies are spawned.
Tip: if you have a script that's named for example "KillField" that you think is causing this, but you don't know what object the script is on, you can write "t:KillField" in the search bar of your hireachy, and you'll get a list of all the objects with that script on it.
When you mean not referenced anywhere... I am instantiating my prefabs without assigning it to any variables/lists. This creates a bunch of GO's at the top level in my GO's hierarchy window, as expected. Does that count as referenced? I would think GC should only affect objects that are inactive. In this case, these objects are part of the game loop and are updating.
The GC, as Baste said, does not include GameObjects. Or any other Unity object. It's completely impossible for GameObjects to disappear on their own; you must explicitly Destroy them yourself.
Not referenced means that it is impossible to access the information anymore. For instance
$$anonymous$$esh mesh=new $$anonymous$$esh();
mesh=new $$anonymous$$esh();
creates 2 $$anonymous$$esh objects and assigns them to mesh. However once the second mesh object is assigned then it is inconceivable that the information from the first object can be accessed. When the garbage collector runs it will notice that there is a block of memory (the first mesh created) that can no longer be used and frees that memory.
Of course, if there is any reference then the object cannot be collected. For instance
$$anonymous$$esh mesh1=new $$anonymous$$esh();
$$anonymous$$esh mesh2=mesh1;
mesh1=new $$anonymous$$esh();
Ends up with the first created mesh being referenced by mesh2 and the second created mesh being reference by mesh1. At the end of the third line both meshes are referenced and the memory used by them will not be recycled.
The garbage collector does not do anything that makes objects disappear. Ins$$anonymous$$d it just gets rid of things that are garbage, thereby freeing space. Since unity has built in references to gameobjects (until they are are destroyed) the garbage collector has nothing to do with your issue.($$anonymous$$esh does not inherit from UnityEngine.Object which is why I used it as an example where garbage collection does not require the use of Object.Destroy())