Destroy object problem

I’m trying to destroy an instantiated prefab, and it’s not working. Here is the code I’m using:

 GameObject go = Instantiate(prefabTrickle, myTransform.position, Quaternion.identity) as GameObject;
 yield return new WaitForSeconds(5);
Destroy(go);

The prefab in question is a legacy particle system, not sure if that makes a difference. Any ideas?

Get rid of as GameObject?

hth

i just give idea to do this:disable the particle effect component Instead of Destroying the components.

If you do that, it will throw an error because you are defining it as a GameObject.

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.

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?

You can use this code

Destroy(gameObject, timeToDestroy);

timeToDestroy is time after you want the gameObject to destroy.

Hope this helps…

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.