Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GameObjects getting Destroyed without calling Destroy()???

Discussion in 'Scripting' started by gian-reto-alig, Feb 20, 2015.

  1. gian-reto-alig

    gian-reto-alig

    Joined:
    Apr 30, 2013
    Posts:
    756
    This is driving me mad for about 8 hours now, but I cannot seem to find a solution.

    I am trying to switch my projectiles from Instantiate() / Destroy() to using a pooling system as it seems the Overhead of the first is quite heavy.

    Now, I have already a pooling system working for ejecting spent cartridges which works quite nice... so I thought this would be an easy task.

    Well, seems I was wrong.
    This is the problem I am having:

    My Projectiles get destroyed after a while without me calling DESTROY()... anywhere in my code!

    It seems to be triggered by the rigidbody getting deactivate / activated, set to kinematic /to non-kinematic, or the whole gameObject getting activate / deactivated when the projectile traveled outside of a given area and should be put into a waiting mode until the projectile gets re-used.


    Now, I know this question might be hard to answer without all the code, but its kind of a mess after my 8 hour session of trying to debug that monster here.

    I have a class that holds an Array of GameObjects as a projectile pool. This gameObject gets accessed by the weapon object to query for the next projectile. I don't set projectiles to used or idle, as I just set the pool size large enough so the projectiles getting re-used have reached their max range anyway.

    This is the method where the projectile objects get returned.
    Code (csharp):
    1.  
    2. public GameObject InstanciateProjectile (Vector3 position, Vector3 direction) {
    3.        if (remainingAmmo > 0) {
    4.          --remainingAmmo;
    5.          ammoCountChanged = true;
    6.  
    7.          GameObject obj = pooledResources[GetNextRef()];
    8.  
    9.          obj.rigidbody.isKinematic = false;
    10.          obj.GetComponent<TrailRenderer>().enabled = true;
    11.          obj.GetComponent<Projectile>().enabled = true;
    12.  
    13.          obj.transform.position = position;
    14.          obj.transform.rotation = Quaternion.LookRotation(direction);
    15.  
    16.          return obj;
    17.  
    18.        } else return null;
    19.      }
    20.  
    And this is the code in the projectile class that handles the deactivation
    Code (csharp):
    1.  
    2.     protected void DeactivateProjectile () {
    3.        obj.rigidbody.isKinematic = false;
    4.        gameObject.GetComponent<TrailRenderer>().enabled = false;
    5.        this.enabled = false;
    6.      }
    7.  

    I am aware that this is not the full code, but as said, the full code is a mess right now, and I need to set the state back to before these changes as I am doing a presentation tomorrow, so I cannot really post clean code today.

    Additional important information about my projectile class:
    - The gameobject has a rigidbody who uses gravity added, a trailrenderer, and a MonoBehaviour which handles the collision detection with raycasts.
    - The rigidbody gets switched to kinematic and the transform moved directly for a short distance when a collision is etected, to make sure the trail really is going up to the position of the collision (as collision detection is handled purely with raycasts). If the projectile bounces off, or hits and later gets reused, it will be set to non kinematic again (and receive a new force applied)

    What I already did / checked:
    - The offending part of the script leading to the projectile getting destroyed seems to be activation / deactivation of some components. It seems only the rigidbody leads to that.
    - I see that the GameObject is being destroyed as I put some debug statements in OnDestroy. I also have seen that by that time, the rigidbody is set to inactive even though it was still active in the method before OnDestroy gets called.

    Is it possible for The game engine to flag GameObjects for destruction? When and Why would it do that? Could messing with the rigidbody really lead to that?

    Thanks for any help in advance!

    EDIT:

    Upon further inspection, these projectiles sometimes seem to just get destroyed without even reaching the point at which their rigidbody should be deactivated...
    The faster the frequency of being fired (like automatic vs single fire), the faster they get destroyed. With fully automatic fire, none of the projectiles seem to make it to their max distance, with the distance until they get destroyed getting smaller and smaller.

    Also, with the MonoDevelop Debugger I am kinda blind, as I seem to be unable to see into what the Unity Engine is doing after FixedUpdate (seems to happen after that).


    Gian-Reto
     
    Last edited: Feb 20, 2015
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Where are you removing items from the pool for use and where are you returning items to the pool after they've done what they need to do?

    My first guess - judging just by the code you have here - is that things aren't being destroyed and instead you're re-using projectiles before they reach their maximum distance. It would make sense then that the problem is compounded by increasing the fire rate. If you're just cycling through an array then you'll get through it faster if you fire at a higher rate.
     
  3. gian-reto-alig

    gian-reto-alig

    Joined:
    Apr 30, 2013
    Posts:
    756
    It makes certainly sense what you are writing, Kelso, and I guess my post up there wasn't clear enough...

    But no, that is not the problem.


    My pooling is not a real pooling system that keeps track of the status of the pooled elements, I only have a single array and basically I just fill the pool with enough elements so the pool never runs out of them even if the rate of fire is fully used, and the projectiles fly up to the max distance.

    The projectiles are indeed destroyed. OnDestroyed() gets called, and I get errors afterwards as soon as my pooling system tries to access these pooled elements (and just recreating them is not really an option, Instanciating and Destroying objects at runtime is what I am trying to avoid with the system).


    It really seems like something outside of my own scripts is interfering and destroying these projectiles. Sadly I cannot really debug that part that well, or at least I don't know how.


    Thanks for the help anyway.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Unless you explicitly call Destroy or DestroyImmediate or you load a new level then the engine won't destroy something for you.
     
    gian-reto-alig likes this.
  5. gian-reto-alig

    gian-reto-alig

    Joined:
    Apr 30, 2013
    Posts:
    756
    So that would mean, if my code didn't destroy it, then it must be a third party asset from the store, right?

    Any good idea how to debug my game globally, so I could see what script / method told the engine to destroy the gameObject? Could the profiler do that? Or am I using the MonoDevelop debugger wrong?
     
  6. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Just a wild guess to rule out everything..Does the TrailRenderer have AutoDestruct flag ?
    Even if it doesn't , maybe you can try disabling each component one by one until
    you find the problematic one.
     
  7. gian-reto-alig

    gian-reto-alig

    Joined:
    Apr 30, 2013
    Posts:
    756
    Yes, that was the problem! Doh! Hasn't occured to me yet that the TrailRenderer has an autodestruct parameter.

    Well, thank you both a lot for you time and help!
     
  8. scourgey

    scourgey

    Joined:
    Jan 5, 2014
    Posts:
    20
    This fixed my issue too! thanks