Search Unity

*Fixed: Solution inside* Particle play script not working

Discussion in 'Scripting' started by Rinz23, Jan 4, 2018.

  1. Rinz23

    Rinz23

    Joined:
    Dec 3, 2017
    Posts:
    4
    Im trying to get a particle system to play from a script, but all the options I found are either decrepated or not working for me. I am sure I am doing something wrong, but I dont no what.
    I have referenced the particle system on the script, and the particle system is working fine, just not with the script.
    If anyone has an idea or example for me to fix this.

    This is basicly the code I found that should be working, but its not...
    Code (csharp):
    1.  
    2.  
    3. public ParticleSystem MuzzleFlash;
    4.  
    5.     ParticleSystem.EmissionModule em = MuzzleFlash.emission;
    6.     em.enabled = true;
    7.  
    8.  
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
  3. Rinz23

    Rinz23

    Joined:
    Dec 3, 2017
    Posts:
    4
    after a bit of try and fail I got it working! Thank you very much Richard!
    This is what I came up with: (I had to use GetComponentInChildren because my particle system is a sub child of the gun: Gun -> dummy helper > particle system
    The shoot function is tied to the fire button offcourse :)

    Code (csharp):
    1.  
    2.    ParticleSystem system
    3.     {
    4.         get
    5.         {
    6.             if (_CachedSystem == null)
    7.                 _CachedSystem = GetComponentInChildren<ParticleSystem>();
    8.            
    9.             return _CachedSystem;
    10.         }
    11.     }
    12.     private ParticleSystem _CachedSystem;
    13.     public bool includeChildren = true;
    14.     void Shoot()
    15.     {
    16.  
    17.         system.Play(includeChildren);
    18.      }
    19.  
     
    richardkettlewell likes this.