Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alphster · Jul 08, 2015 at 09:30 AM · gameobjectruntimedisappear

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nerevar · Jul 08, 2015 at 09:33 AM 0
Share

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.

avatar image NoseKills · Jul 08, 2015 at 09:49 AM 1
Share

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.

avatar image meat5000 ♦ · Jul 08, 2015 at 12:09 PM 0
Share

Do they have a projectile?

avatar image alphster · Jul 08, 2015 at 10:38 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alphster · Jul 08, 2015 at 10:36 PM 0
Share

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.

avatar image Eric5h5 · Jul 08, 2015 at 10:56 PM 0
Share

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.

avatar image maccabbe · Jul 08, 2015 at 11:26 PM 0
Share

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())

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Having trouble swapping GameObjects on button click 1 Answer

Unity RunTime hole 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Assign mouse events for gameobject/collider at runtime 1 Answer

transforming a prefab randomly at runtime 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges