Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to use the Particle System Prefabs?

Discussion in 'Scripting' started by kerrmedia, Feb 18, 2016.

  1. kerrmedia

    kerrmedia

    Joined:
    Nov 4, 2015
    Posts:
    250
    Not finding anything useful in the manuals, just older tutorials for the particle assets.
    Can anyone show me how to use the new Particle System prefabs in a C# script or tell me how to find previous posts where this was explained?
     
  2. kerrmedia

    kerrmedia

    Joined:
    Nov 4, 2015
    Posts:
    250
    there is an Explosion prefab in the Particle Systems asset folder. I tried this:

    Code (CSharp):
    1. GameObject e = Instantiate(Explosion, hitInfo.collider.gameObject.transform, hitInfo.collider.gameObject.transform.rotation);
    2.                     Destroy(e);

    but getting this message:

    Error 1 The name 'Explosion' does not exist in the current context
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    going to need to see more code than that, what is "Explosion" where is it declared/instantiated etc.

    Also instantiating and immediately destroying a thing will likely mean it wont exist long enough to do anything much...
     
  4. kerrmedia

    kerrmedia

    Joined:
    Nov 4, 2015
    Posts:
    250
    right, the prefabs are in the standard package assets "Particle Systems", but the only examples I can find are for starting from scratch?
     
  5. kerrmedia

    kerrmedia

    Joined:
    Nov 4, 2015
    Posts:
    250
    particle_prefab.jpg

    Just like to see a code sample of how to instantiate and use it?
     
  6. popMark

    popMark

    Joined:
    Apr 14, 2013
    Posts:
    114
    You'll want a public variable to reference that explosion prefab

    add
    Code (CSharp):
    1. public GameObject Explosion;
    to your class, add that class as a component to a gameobject in your scene if you haven't already, then drag the Explosion prefab into the Explosion object field in the components inspecotor
     
    Ice_ZOMBIES likes this.
  7. kerrmedia

    kerrmedia

    Joined:
    Nov 4, 2015
    Posts:
    250
    Very different, but cool. Never would have guessed dragging into the public variable of the script.

    So this makes the explosion show up and run great, but do I need to destroy the object or is that handled by the object itself? Should I make the particle instance a global and destroy it in a coroutine? How would I know when it has stopped playing? Or is there a way to keep resetting and resuing it? As a generic Object not sure how to access any of the particle prefab properties.
    Oh yeah, thank you so much for your help!

    Code (CSharp):
    1. Object e = Instantiate(Explosion, hitInfo.point, Quaternion.identity);
    2.                
     
    Last edited: Feb 19, 2016
  8. Ice_ZOMBIES

    Ice_ZOMBIES

    Joined:
    Jan 30, 2022
    Posts:
    1

    6 years later popMark and your comment is still helping people homie.

    You just helped me figure out how tf to do this after spending DAYS on it. I kept trying to code my script to call the prefabs script rather than the prefab itself and dragging it into the script.

    Thank you so much! You've saved me and my computer science homework assignment.
     
  9. lucassaldanha

    lucassaldanha

    Joined:
    Mar 11, 2014
    Posts:
    3
    Check the particle system object, in the inspector you should see a “Stop Action”. From there you can pick what happens when it finished (including destroy).

    Check this answer for some screenshots: https://answers.unity.com/questions/1755602/how-to-destroy-particle-gameobject-after-its-finis.html#