# problem creating instance of prefab

**URL:** <https://discussions.unity.com/t/problem-creating-instance-of-prefab/498680>\
**Category:** Asset & Content Management\
**Tags:** Scripting\
**Created:** [February 28, 2013, 7:09pm UTC](https://discussions.unity.com/t/problem-creating-instance-of-prefab/498680 "2013-02-28T19:09:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![surreal4224](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@surreal4224](https://discussions.unity.com/u/surreal4224)\
**Post date:** [February 28, 2013, 7:09pm UTC](https://discussions.unity.com/t/problem-creating-instance-of-prefab/498680/1 "2013-02-28T19:09:17Z")

</div>

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

```auto
#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:

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

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

```

Thanks a lot for your help

---

<div class="post-metadata">

**Author:** ![Lost\_Artist](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@Lost\_Artist](https://discussions.unity.com/u/Lost_Artist)\
**Post date:** [February 28, 2013, 7:13pm UTC](https://discussions.unity.com/t/problem-creating-instance-of-prefab/498680/2 "2013-02-28T19:13:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Vanamerax](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/vanamerax/32/223955_2.png) [@Vanamerax](https://discussions.unity.com/u/Vanamerax)\
**Post date:** [February 28, 2013, 7:17pm UTC](https://discussions.unity.com/t/problem-creating-instance-of-prefab/498680/3 "2013-02-28T19:17:04Z")

</div>

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

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

```

or try

```auto
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

```auto
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

---

<div class="post-metadata">

**Author:** ![hpjohn](https://avatars.discourse-cdn.com/v4/letter/h/b782af/32.png) [@hpjohn](https://discussions.unity.com/u/hpjohn)\
**Post date:** [February 28, 2013, 8:33pm UTC](https://discussions.unity.com/t/problem-creating-instance-of-prefab/498680/4 "2013-02-28T20:33:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![surreal4224](https://avatars.discourse-cdn.com/v4/letter/s/f0a364/32.png) [@surreal4224](https://discussions.unity.com/u/surreal4224)\
**Post date:** [February 28, 2013, 11:35pm UTC](https://discussions.unity.com/t/problem-creating-instance-of-prefab/498680/5 "2013-02-28T23:35:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![THENATHE](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@THENATHE](https://discussions.unity.com/u/THENATHE)\
**Post date:** [February 28, 2013, 11:39pm UTC](https://discussions.unity.com/t/problem-creating-instance-of-prefab/498680/6 "2013-02-28T23:39:22Z")

</div>

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