Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  3. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Game Object being destroyed, why?

Discussion in 'Physics' started by Pheonix1977, Mar 25, 2015.

  1. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
    Hello All,

    I am really hoping someone can help me. My game is running to slow on 1st and 2nd generation iOS devices with CPU pegged out at 100%. So I started to do some optimizations, one being pooling all of the bullets being instantiated and destroyed. I started with my turret bullets and for some reason when they collide with something they are being destroyed. I have "find in files" of my entire project and commented out all of the Destroy(... statements, all of them, and the bullet is still being destroyed. Does anybody have any ideas why? Is there a setting I do not know about in the Unity editor? Thank you in advance. ConsoleScreenshot.png CodeOfTurretBulletDeactivate.png
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    You are starting an Invoke. If the bullet gets destroyed before 5 seconds are passed, the bulletHide method is still executed, which lead to a null reference exception.
    Alternatively you can start a coroutine which waits 5 seconds. As far as I know, coroutines are automatically terminated when the game object is destroyed. If not, you can just stop the coroutine.
     
  3. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
    Thank you for your quick reply. The problem is, I do not want the bullet to be destroyed. I just want to hide it after 5 seconds, which I commented out the code for now so I can move the bullet slowly trying to figure this out. As soon has it touches the ship the bullet is destroyed, but there is not script that should destroy it.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    You are definitely destroying the bullet somewhere. You may search in your code for "Destroy".
     
  5. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
    I have commented out every Destroy(...); statement in my whole entire project. Still being destroyed.... Playing with the numbers more, it even destroys itself without a collision. Any other ideas???
     
  6. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
    An update....I think I found the line of code that is causing the destruction of my bullet. I am assigning a Vector3 to the Rigidbody.velocity. If this code is commented out, the bullets are not destroyed. When I uncomment this line, the bullets are destroyed. Why would assigning a velocity to the Rigidbody cause the game object to be destroyed?
     
  7. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
    Another interesting update. I changed the code to use transform.position instead of rigid body.velocity and my bullets are no longer being destroyed. I really believe that there is a bug in the rigid body velocity property causing the destruction of my game objects. Anyone have thoughts on this?
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    Because it might be destroyed on collision. So there is a Destroy or DestroyImmediate in your code somewhere that is responsible for that
     
  9. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    732
    DestroyImmediate too?
     
  10. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
    Unfortunately have never used DestroyImmediate in an script in my project.
     
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    Do you use any 3rd party extensions which contain scripts? There has to be something that destroys the bullet, because this never happens automatically.
     
  12. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
    No...I did not use any 3rd party code. I wrote it all myself. I just did a different test. Without changing any code at all, I created another prefab. Just a plain old capsule, my bullet is just a shere with a material. I attached the same rigid body and scripts to the new capsule prefab and set the tag and layer to be the same as my bullet prefab. The capsule prefabs are never destroyed. The game objects look identical with the same components and same scripts. I am thinking that my bullet prefab has some type of corruption. Is this possible? CapsuleLog.png CapsuleLog.png BulletLog.png
     
  13. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
    I just upgraded my project to Unity 5....maybe the upgrade process did something?
     
  14. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    It is very unlikely that game objects are automatically destroyed after the upgrade to Unity 5. Did you check ALL your scripts whether they contain Destory(Immediate)?
     
  15. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
  16. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    Can you share your project?
     
  17. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
    I prefer not to share my whole project. I have a work around solution by deleting the prefabs that are being destroyed and recreating them. I have done this with the turret bullet and it is working like it should now. A little extra work, oh well. It is frustrating not knowing why though.
     
  18. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,668
    It is not possible to help without further information, the actual code or the actual project...
     
  19. Pheonix1977

    Pheonix1977

    Joined:
    Jun 23, 2014
    Posts:
    13
    I have recreated all of my prefabs and the problem is gone. Thanks everyone for your assistance.
     
  20. LordBlackwood

    LordBlackwood

    Joined:
    Aug 10, 2013
    Posts:
    26
    Just so you don't feel like you were going crazy. I'm having this exact same issue. No destroy statements anywhere in the project. No third party plug ins. Was trying to implement object pooling and for some reason on collision my projectiles are destroyed.

    No idea what I've missed.
     
    ilmario likes this.
  21. Drumsmasher17

    Drumsmasher17

    Joined:
    Jun 3, 2015
    Posts:
    3
    Posting for future visitors: I too had the same issue as the two above but fixed it after removing a Trail Renderer component from my bullets. I'm going to guess that it can't handle being instantly repositioned. There doesn't seem to be a Unity error message for that event, and there's "barely" a warning about it in the documentation for it.

    If you want to still have a trail renderer, I'm going to guess that you need to disable it when you shoot and then re-enable it on the subsequent frame possibly, though I haven't tested doing that yet

    EDIT/UPDATE*:
    Turns out neither disable/re-enabling the trail renderer works nor the TrailRenderer.Clear() function, the object that has the trail renderer still gets deleted (suggesting the latter function is bugged in some way). Also found out having a more solid/longer trail resulted in less likely deletions of my bullets, so I suspect the issue arises when unity tries to calculate the points on a trail between two big distances (common when repositioning bullets for a pool) and fails when it comes to calculating the alphas for the materials (maybe, I'm not an expert)
     
    Last edited: Jan 27, 2016
  22. Twosheds-Jackson

    Twosheds-Jackson

    Joined:
    Mar 1, 2014
    Posts:
    1
    I've run into mysterious object destruction before. One thing you need to make sure of is that the object which is being mysteriously destroyed is not a child of another object (via the transform.parent mechanism). If it is, then if the parent is destroyed, the child will be destroyed as well, even if other things are holding references to the object.
     
  23. Lorshank

    Lorshank

    Joined:
    Feb 14, 2017
    Posts:
    1
    FYI, I'm also having this problem when I set the alpha on my material to 0. Or when I set renderer.enabled to false.
     
  24. HaroldReyiz

    HaroldReyiz

    Joined:
    Oct 20, 2017
    Posts:
    1
    Thank you so much. Disabled the Trail Renderer on my prefab and poof, the problem's gone! I immediately thought okay than I'll disable the bullets when they get inactive and reenable them when they will be used again (object pooling). But seeing as you already tried it, it's a little discouraging. I'll try to get around this and post here if I succeed.

    EDIT:
    Solution is to just uncheck the AutoDestruct property of the Trail Renderer on the inspector.
    According to its manual, Trail Renderer destroy the gameobject after Time (another property) seconds if it was idle. By default it comes with AutoDestruct = true.
     
    Last edited: Dec 24, 2017
    VadimPyrozhok likes this.
  25. rastlin

    rastlin

    Joined:
    Jun 5, 2017
    Posts:
    70
    I had a similar "problem", the issue was the settings I set for my Particle System in parent object. On certain event I attach an FX object with particle system as parent to my game object.

    Due to my negligence I kept 'Stop Action' on my particle system as 'Destroy' which caused my game object to be destroyed when the FX has ended animating. Silly me :) .
     
  26. RencendStudios

    RencendStudios

    Joined:
    Jul 27, 2016
    Posts:
    1
    I can confirm that the trail renderer is the cause of this, I guess if you want a trail you need to get a different asset.
     
  27. jlubeck

    jlubeck

    Joined:
    Jun 14, 2015
    Posts:
    4
    Thank you!!! AutoDestruct was the culprit