problem creating instance of prefab

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);

Thanks a lot for your help

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 :slight_smile: would be educaitonal.

In what language are you programming? You seem to switch C# and Javascript around. In C# this should work fine:

GameObject newDust = (GameObject)Instantiate(dustCloud,transform.position,transform.rotation);

or try

GameObject newDust = Instantiate(dustCloud,transform.position,transform.rotation) as GameObject;

I think in javascript it’s pretty much the same, but you should keep the same syntax as the rest

var newDust : GameObject = (GameObject)Instantiate(dustCloud,transform.position,transform.rotation);

EDIT: also, if you’re going to use the same particle system frequently, I’d advise to just disable the particles untill you need them again

var newDust : GameObject = Instantiate(dustCloud,transform.position,transform.rotation);
Destroy(newDust,3);

Should be fine?
Also you haven’t said if its legacy particles or shuriken, though this won’t have any bearing on the function of Destroy()

So I figured out a solution.

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.

Hopes this helps

Try just setting active, not sure how you would do this, so try checking the scripting reference. That’s my 2 cents.