Search Unity

Destroy object problem

Discussion in 'Scripting' started by Nomad72, Jul 25, 2012.

  1. Nomad72

    Nomad72

    Joined:
    Oct 25, 2011
    Posts:
    117
    I'm trying to destroy an instantiated prefab, and it's not working. Here is the code I'm using:

    Code (csharp):
    1.  
    2.  GameObject go = Instantiate(prefabTrickle, myTransform.position, Quaternion.identity) as GameObject;
    3.  yield return new WaitForSeconds(5);
    4. Destroy(go);
    5.  
    The prefab in question is a legacy particle system, not sure if that makes a difference. Any ideas?
     
  2. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853


    Get rid of as GameObject?

    hth
     
  3. UNITY3D_TEAM

    UNITY3D_TEAM

    Joined:
    Apr 23, 2012
    Posts:
    720
    i just give idea to do this:disable the particle effect component Instead of Destroying the components.
     
  4. Nomad72

    Nomad72

    Joined:
    Oct 25, 2011
    Posts:
    117
    If you do that, it will throw an error because you are defining it as a GameObject.
     
  5. Nomad72

    Nomad72

    Joined:
    Oct 25, 2011
    Posts:
    117
    That may work... but it sort of ignores the real issue: I don't need or want the particle effect anymore, so destroying it makes sense and this code should destroy the gameObject, but does not.

    Im reluctant to go the "disable" route, as I could land up with hundreds of these "disabled effects" which really aren't needed anymore.
     
  6. znoey

    znoey

    Joined:
    Oct 27, 2011
    Posts:
    174
    means you don't enjoy optimizing, pooling and is an incredibly efficient way to handle objects in OOP, what your saying is you don't want to pool anything.

    Try using DestroyImmediate( object );
    or instead of casting to GameObject, try UnityEditor.Object. However its weird to see it not working. I know calling Destroy doesn't necessarily mean its out of memory with managed code, but being that its a unity object I'm pretty sure those aren't garbage collected anyway.

    Are you sure its making it through the coroutine?
     
  7. MicroEyes_old

    MicroEyes_old

    Joined:
    Jun 9, 2011
    Posts:
    188
    You can use this code

    Destroy(gameObject, timeToDestroy);

    timeToDestroy is time after you want the gameObject to destroy.

    Hope this helps..
     
  8. Nomad72

    Nomad72

    Joined:
    Oct 25, 2011
    Posts:
    117
    I guess I could try pooling, but it seems unlikely to make any real difference: it's a tiny object to instantiate and I can't believe that instantiating/destroying it would be significantly worse performance-wise than using an object pool. I haven't tested pooling here, so I'm just guessing about the lack of significant performance difference.

    And yes, it makes it through the coroutine. I've only shown the code related to the attempt to destroy it, but everything else works exactly as expected.

    DestoryImmediate() makes no difference, neither does instantiating as an Object instead of a GameObject. It's very puzzling.