Add particles on button click in unity

Nibha Jain picture Nibha Jain · Jun 8, 2017 · Viewed 9.9k times · Source

I am new to unity 3d, I have to do some enhancement in exiting project.. if user choose correct option then I have to show some particles around the button at runtime. My code for adding particles is below ..not working:

ParticleSystem ps = GetComponent<ParticleSystem>();
ps.Play ();

I have also added particles component from unity editor..

Thanks in advance

Edit :

as @kardux suggested:

declaration :

[SerializeField] private ParticleSystem ps;

on method :

ps.Play()

Screenshot from inspector:

enter image description here

Error:

I/Unity   (23313): NullReferenceException
I/Unity   (23313):   at UnityEngine.ParticleSystem.<Play>m__0 (UnityEngine.ParticleSystem ps) [0x00001] in /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:3666 
I/Unity   (23313):   at UnityEngine.ParticleSystem.IterateParticleSystems (Boolean recurse, UnityEngine.IteratorDelegate func) [0x00003] in /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:3780 
I/Unity   (23313):   at UnityEngine.ParticleSystem.Play (Boolean withChildren) [0x00020] in /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:3666 
I/Unity   (23313):   at UnityEngine.ParticleSystem.Play () [0x00005] in /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:3661 

Answer

Kardux picture Kardux · Jun 8, 2017

First of all if you're using particles inside Unity UI I highly advise you looking to UIParticleSystem.cs script from Unity UI Extension repository: this is a community gathering of many useful UI tools :)
(simply don't forget to add the UI/Particles/Hidden shader that you can find here)

You can change the sprite you want to use here: UIParticleSystem component settings

Also keep in mind when using this script that you will have to scale your particles according to your screen (particles are initialized at a size of 1 because that's 1 meter in Unity 3D world: but now you will probably be in canvas space which will be something like 1920x1080px so 1px will be very small). You can find some base settings below: ParticleSystem component settings

Now coming to your scrip I suspect you simply have to call Stop() before Play() like this (note I used a burst emission type in my particle system settings):

ParticleSystem ps = GetComponent<ParticleSystem>();
ps.Stop ();
ps.Play ();

P.-S. Please note that if you use UIParticleSystem script you will have to consider your particles system as an UI item (will be rendered on top of other items based on hierarchy order)

Hope this helps,

EDIT:
You have two ways of setting up your GameObjects:

  • you have all component on the same GameObject (ParticleSystem, UIParticleSystem and YOUR_SCRIPT): this way you can get the ParticleSystem reference by calling GetComponent<ParticleSystem>() inside your script

  • you have one particle GameObject (with ParticleSystem and UIParticleSystem) and YOUR_SCRIPT is on another GameObject: you can't call GetComponent<ParticleSystem>() in your script since it will search on the components of this GameObject so you declare a ParticleSystem ps; variable (either public or [SerializeField] private) that you assign through the Inspector by dragging your particle GameObject to it.

Note that implicitly, GetComponent<ParticleSystem>() equals this.gameObject.GetComponent<ParticleSystem>(): that's why it will search components from the current GameObject.

EDIT 2:
Not sure why your script throw this NullReference exception: I just tried with a very short script and it works perfectly...

public class TestScript: MonoBehaviour
{
    [SerializeField]
    private ParticleSystem ps;

    void Start()
    {
        // This one is not even needed
        ps.Stop();
    }

    public void PlayParticles()
    {
        ps.Stop();
        ps.Play();
    }
}