0

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 
Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
  • For a particle system to start emitting particles you have to set the number of particles you want to [emit](https://docs.unity3d.com/ScriptReference/ParticleSystem.Emit.html). In other words you could do `ps.Emit(10);` before calling the `Play` function. – Hristo Jun 08 '17 at 10:17
  • I tried using this for ps.Emit( (int) (75 * Time.deltaTime) ); but it was not working ... – Nibha Jain Jun 08 '17 at 10:31
  • 1
    @Hristo you don't need to set the emission in code. You should design it in editor. – Dávid Florek Jun 08 '17 at 10:45

4 Answers4

2

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();
    }
}
Kardux
  • 2,117
  • 1
  • 18
  • 20
  • Just a quick note on that: it seems the problem comes from the _Play On Awake_ property of the particle system. If you test `ps.isPlaying` value before calling `Stop()` in my script, it will be true even the first time (with _Play On Awake_ unchecked). – Kardux Jun 12 '17 at 07:44
  • Thanks @Kardux I am getting NullReferenceException: Object reference not set to an instance of an object on ps.Stop()..do you have idea on this? – Nibha Jain Jun 14 '17 at 10:06
  • Is the script on the same object as your **ParticleSystem** component ? If not you have to declare `[SerializeField] private ParticleSystem ps;` as a variable of your scrip and assign it via the Inspector (or you can just move the script to the same object as your **ParticleSystem**). – Kardux Jun 14 '17 at 10:10
  • I created a gameobject ParticleSystem then added UIParticleSystem.cs this scrip to that object ... and assigned ParticleSystem to particles under a panel from editor ..not the code above mentioned I added on the required method... I am completely new to unity ..is it right way? – Nibha Jain Jun 14 '17 at 10:14
  • I just edited my answer to explain you a bit how you can have a working reference to your **ParticleSystem** component: tell me if you can make it work :) – Kardux Jun 14 '17 at 11:02
  • I posted a short testing script: can you tried it ? Other than that, are you sure you don't override the `ps` reference anywhere else (by calling `ps = GetComponent()` for example) ? You can test it by checking the value of your `ps` variable in Inspector after the _NullReference_ exception occurs. – Kardux Jun 14 '17 at 11:58
0

Provided you have a particle system on the same gameObject as the script that is calling it, it should be fine.

Are you using a UI button? If so, have a look here.. http://answers.unity3d.com/questions/852397/particle-system-in-46-ui.html

It's old but still relevant.

Ben Brookes
  • 419
  • 4
  • 15
0

Are you using the New Unity UI System or GUI, Is the UI worldspace?

  1. Create a empty gameobject - attach the particle to that.
  2. Whenever you want emit particles call Gameobject.SetActive(true);
  3. Make sure Play on Awake option is checked in the particle system.

Set the position of the particle according to your UI.

rohankad
  • 79
  • 7
  • I am using unity 5.6 editor.. have added game object and particle system to that , play on awake option is checked.. so I just need to call Gameobject.SetActive(true); on scripts desired condition at runtime? – Nibha Jain Jun 08 '17 at 10:29
  • Yes, Initially disable the particle system gameobject. Whenever you need to emit the particles just enable it for the required time. Add the particle to separate gameobject not for the UI. Are you sure the particle system is working fine, play it in the editor to check if its working. – rohankad Jun 08 '17 at 10:36
  • I am able to add particles on static elements ... or on home page..but not where I required at runtime! – Nibha Jain Jun 08 '17 at 10:46
  • Having particle system emitting all the time and just disable it is bad practice. Instead use `ParticleSystem.Play` and `ParticleSystem.Stop`. – Dávid Florek Jun 08 '17 at 10:47
  • Above code n my question is not working... any suggestion on that? – Nibha Jain Jun 08 '17 at 11:03
  • Create a prefab of the gameobject with particle and instantiate it in runtime. – rohankad Jun 09 '17 at 14:11
0

public GameObject particle;

//include the particle in the gameobject

void Start() {
  particle.SetActive(false);
 }

void button() {
 particle.SetActive(true);
}

//this works.