Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Playing A Particle System Through Script (C#)

Discussion in 'Scripting' started by Nikola310, Dec 23, 2015.

  1. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Hello, everyone.
    Let me get straight to the point:
    My game is advanced Roll A Ball, and I want to add particles when falling off a platform.
    My DeathFall script (that should play the particles when falling, and restart the scene after that- Y -60 to be exact) is a component of the Player, and so is the ParticleSystem.
    I want the ParticleSystem to play between Y -20 and Y -60. Here is my DeathFall script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class DeathFall : MonoBehaviour {
    6.  
    7.     public ParticleSystem tpc;
    8.  
    9.     void Start()
    10.     {
    11.         tpc = gameObject.GetComponent<ParticleSystem> ();
    12.         tpc.emissionRate = 0;
    13.         tpc.enableEmission = false;
    14.         // None of this is working!
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if(gameObject.transform.position.y <= -20 && gameObject.transform.position.y >= -60)
    20.         {
    21.             tpc.emissionRate = 1;
    22.             // This neither!
    23.             tpc.Play();
    24.             // And this doesn't work too!
    25.         }
    26.         if(gameObject.transform.position.y <= -60)
    27.         {
    28.             SceneManager.LoadScene(Application.loadedLevel);
    29.             //This restarts the game
    30.         }
    31.     }
    32. }
    I've attached the ParticleSystem component of the Player in the "Tpc" slot of the DeathFall script component in the Player.
    Thank you in advance.
     
  2. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    236
    I am having the same issue.

    We have some functions that should play all particlesystems on a gameObject (and its children).
    E.g. you have an explosion with multiple (different) particlesystems as children on it. All the systems have playOnAwake set to false.

    The same issues as yours occur.

    However, when I change the Simulation Space to World the particles do play!
    In our scene it seems all particle systems set to Local are causing issues. They all seem incorrect (position, orientation, etc.)

    There is nothing (useful) mentioned in the 5.3 upgrading guide either. This really feels like a bug.


    Edit: While looking again - it seems - in my case that the particle system is only played once!
    I was testing the issue with a muzzleflash particle system which was really short so I missed the first play. All subsequent calls to play are ignored.
     
    Last edited: Dec 24, 2015
  3. Recon03

    Recon03

    Joined:
    Aug 5, 2013
    Posts:
    842
    Did you debug it and make sure its at that coords your looking for in your if statement? That would be the first step to make sure that happens.. or not.
     
  4. Recon03

    Recon03

    Joined:
    Aug 5, 2013
    Posts:
    842
    Code (CSharp):
    1. public float engineRevs;
    2.         public float exhaustRate;
    3.  
    4.         ParticleSystem exhaust;
    5.  
    6.  
    7.         void Start () {
    8.             exhaust = GetComponent<ParticleSystem>();
    9.         }
    10.    
    11.  
    12.         void Update () {
    13.             exhaust.emissionRate = engineRevs * exhaustRate;
    14.         }
     
  5. Recon03

    Recon03

    Joined:
    Aug 5, 2013
    Posts:
    842
    Code (CSharp):
    1. ParticleSystem part = GetComponent<ParticleSystem>();
    2.   part.enableEmission = true;

    This is another way you can try.
     
    CrandellWS likes this.
  6. Vanl

    Vanl

    Joined:
    Jun 5, 2015
    Posts:
    1
  7. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Still not working..
    Do I need to assign any values to engineRevs and exhaustRate in the inspector?
    Sorry,I'm still new to Unity.
     
  8. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    I put it to World Simulation Space and it still isn't working.. I also don't know how to make explosions, so I only have one (stupid) ParticleSystem component attached to the player(not as a seperate Game Object).
     
  9. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    236
    I am doing this on the top off my head.
    But isn't enableEmission deprecated in 5.3? Shouldn't it be something like part.emission.enable = true; ??
    Not sure if this works though!

    I was testing a bit more last week as well. I another thread a workaround was mentioned.
    You would have to enable subemitters in the particle system (there is no need to actually add a subemitter though)

    I have had mixed results with this workaround though. In the end I gave up and I continued work on other parts of the project. It still needs to be done ;)

    In hindsight I might not have switched to 5.3(.1) so quickly. For us this has been the most unstable release for our project thus far.
     
  10. j.leslie

    j.leslie

    Joined:
    Apr 2, 2013
    Posts:
    1
    Unity dev team....Please Update Particles emission API!!!

    this issue is Very Critical...