I am working with prefab particle systems and once they are instantiated they stick around after their use, so I need to remove them once they have done their job.
Been reading up and trying various ways to destroy my prefab with no success. I know I need to create an instance of the prefab and target it as a game object, but been failing to find a way that doesnt produce compile errors
#pragma strict
var dustCloud : Transform;
function OnCollisionEnter(collision: Collision){
var contact : ContactPoint = collision.contacts[0];
var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal);
var pos : Vector3 = contact.point;
Instantiate(dustCloud,pos,rot);
var newDust : GameObject = GetComponent(dustCloud.particleSystem);
Destroy(newDust,3);
//Instantiate(dustCloud, pos, rot);
//DestroyImmediate(dustCloud);
}
other failed attempts:
GameObject newDust = (GameObject)Instantiate(dustCloud,transform.position,transform.rotation);
var newDust : GameObject = Instantiate(dustCloud,transform.position,transform.rotation);
I was doing this too, I made particles come out of a chest on opening; however realised prefabs can’t be instantiated… or was simply too complex for me to do it haha;
I made my own particle system within unity; and applied this; it worked fine.
I tried it with Unities built in particle prefabs, some from the asset store and still no success. - if you do manage it let me know via a pm would be educaitonal.
you can destroy prefabs and gameObjects as long as they are themselves. Unity does not like destroying other prefabs or gameobjects that you referenced with your inspector.
create a script and add it to the prefab or gameObject you wish to destroy and this is where the destroy() function worked for me . Obviously do some testing to check if you want to destroy first, or else it will be destroyed the moment its Instantiated.