Search Unity

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,667
    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,667
    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?
     
    Gabe-Tucker likes this.
  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?
     
    Gabe-Tucker likes this.
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    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:
    781
    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,667
    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,667
    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,667
    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,667
    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:
    7
    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:
    2
    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:
    3
    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
  25. rastlin

    rastlin

    Joined:
    Jun 5, 2017
    Posts:
    127
    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
     
    keshav2010 likes this.
  28. arjuniscool0204

    arjuniscool0204

    Joined:
    May 16, 2017
    Posts:
    14
    Lol, i was making a multiplayer game when i got stuck and came here. The mistake i made was i was reloading the scene everytime the client disconnected from the game in the network manager component. Haha i thought I found a new bug.(I guess unet is deprecated so this aint that useful but just to help any1 who might need to know)
     
  29. scorpioservo

    scorpioservo

    Joined:
    Jul 24, 2020
    Posts:
    13
    This was my exact issue too! Thank you so much for posting!
     
  30. laserbean00001

    laserbean00001

    Joined:
    Aug 23, 2020
    Posts:
    4
    In 2021, same problem. But I'm using 2019 unity. Thank you very much.
     
  31. suleymanbucuk

    suleymanbucuk

    Joined:
    Mar 13, 2019
    Posts:
    11
    TrailRenderer compenent is causing this in my project. After i added a Trail Renderer to my space ship, i wanted it to be active only when my space ship is dead and falling down in space. So i used

    _trail = GetComponent<TrailRenderer>();
    _trail.enabled = false;

    at start method. When i play the scene, my spaceship is all disabled at the start. So i decided to use a code at Start() same as below to disable trail effect:

    _trail.time = 0f;
    where i decide to enable effect when space ship is dead and falling down in space:

    _trail.time = 0.55f;

    This has been assumed to happen as what it says, but what happened is when i move my spaceship, it' s being destroyed completely. ( I used neither any Destroy() DestroyImmediately() func nor any third party API including code)

    So all i can do here is to agree with great developers pointed above, trail renderer component has real bugs at the moment.(Using 2019.4.9 - was LTS when i began my project)
     
  32. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Please note that this is the physics forums for physics related posts. The TrailRenderer has nothing whatsoever to do with physics.

    I would suggest reporting a bug and/or posting on the graphics forums regarding this issue where you're more likely to attrack devs who deal with this component.