Disable and enable particlesystem.

Hi, I’m trying to disable and enable a Particle System. At the docs I read the following:

 ParticleSystem ps = GetComponent<ParticleSystem>();
        var em = ps.emission;
        em.enabled = true;

I have implemented this code in my script. The particle system and following script is attached to my player. When the player jumps and lands the particle system should play dust particles on landing and on a OnTriggerEnter event that is attached to a floor object. The particle system has a collider. At the moment the code below isn’t working. The particle system still starts emitting on start.

I’m getting this error:
“NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance”

Anyone any suggestion or can see what I did wrong? Still new to Unity.

The OnTriggerEnter event is listed below in the code where the particles get activated:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerJumpScript3D : MonoBehaviour {


    public static PlayerJumpScript3D instance;

    private Rigidbody myBody;
    private Animator anim;
    public ParticleSystem dustParticle;

    [SerializeField]
    private float forceX, forceY;
    private float tresholdX =7f;
    private float tresholdY = 14f;
    //Treshold was X 7f en Y 14f

    private bool setPower, didJump;
    private bool isGrounded = true;

    private Slider powerBar;
    private float powerBarTreshold = 10f;
    private float powerBarValue = 0f;


    void Awake ()
    {

        transform.Rotate(0,90,0);
        MakeInstance();
        Initialize();
        SetPower();
    }



    void Update ()
    {
        SetPower();
    }


    void Initialize ()
    {
        powerBar = GameObject.Find ("Power Bar").GetComponent<Slider>();
        myBody = GetComponent<Rigidbody> ();
        anim = GetComponent<Animator> ();
        ParticleSystem dustParticle = GetComponent<ParticleSystem>();
        var dustPrtcl = dustParticle.emission;
        dustPrtcl.enabled = false;


        powerBar.minValue = 0f;
        powerBar.maxValue = 10f;
        powerBar.value = powerBarValue;
    }



    void MakeInstance ()
    {
        if(instance == null)
            instance = this;


    }


    void SetPower ()
    {
        if (setPower) {
            forceX += tresholdX * Time.deltaTime;
            forceY += tresholdY * Time.deltaTime;

            //if(forceX > 6.5f)
                //forceX = 6.5f;

            //if(forceY > 13.5f)
                //forceY = 13.5f;


            if(forceX > 6.5f)
                forceX = 6.5f;

            if(forceY > 13.5f)
                forceY = 13.5f;


            powerBarValue += powerBarTreshold * Time.deltaTime;
            powerBar.value = powerBarValue;
        }
    }

    public void SetPower (bool setPower)
    {
        this.setPower = setPower;

        if (!setPower) {
            Jump();
        }

    }


    void Jump ()
    {

        if (isGrounded)
        {

            myBody.velocity = new Vector3 (forceX, forceY);
        forceX = forceY = 0f;
        didJump = true;

        anim.SetBool("Jump", didJump);

        powerBarValue = 0f;
        powerBar.value = powerBarValue;
    }

    }


    void OnTriggerEnter (Collider target)
    {

        if (didJump) {

            didJump = false;
            anim.SetBool("Jump", didJump);

            if (target.tag == "Platform") {

                isGrounded = true;


                if (GameManager.instance != null) {
                    GameManager.instance.CreateNewPlatformAndLerp (target.transform.position.x);
                    var dustPrtcl = dustParticle.emission;
                 
                    dustPrtcl.enabled = true;
                }


                if (ScoreManager.instance != null /*&& forceX > 0.1f && forceY > 0.1f*/) {
                
                    ScoreManager.instance.IncrementScore();
                    ScoreManager.instance.HighScore ();
                }



            }
        }


        if (target.tag == "Dead") {
            if (GameOverManager.instance != null) {
                GameOverManager.instance.GameOverShowpanel();
            }

            Destroy(gameObject);
        }

    }

    private void OnTriggerExit(Collider target)
    {
        if (target.tag == "Platform")
            isGrounded = false;
    }
}

Ok, I made some little progress after 3 hours searching the web :smile:. That happens when you are new :hushed:

I got the instantiate working without errors now. The game character starts with an instance of the Particle System and I can deactivate it’s emission on start. But the particle dust now needs te be instantiated after the character lands on the ground after a jump and should play a little dust particle that won’t follow the character.

But I think I shouldn’t be working with emission true or false, because the particle system doesn’t loop. So should i instantiate it every time hitting the ground and then destroy it?

Can someone help me in the right direction?

The code so far is this:
The OnTriggerEnter event on line 135 is where the Particles should start, that is the players landing event on the ground.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerJumpScript3D : MonoBehaviour {


    public static PlayerJumpScript3D instance;

    private Rigidbody myBody;
    private Animator anim;

    //Setting the ParticleSystem vars for dust at landing on ground
    public GameObject dustPuff;
    private ParticleSystem dustParticle;

    [SerializeField]
    private float forceX, forceY;
    private float tresholdX =7f;
    private float tresholdY = 14f;
    //Treshold was X 7f en Y 14f

    private bool setPower, didJump;
    private bool isGrounded = true;

    private Slider powerBar;
    private float powerBarTreshold = 10f;
    private float powerBarValue = 0f;


    void Awake ()
    {

        transform.Rotate(0,90,0);
        MakeInstance();
        Initialize();
        SetPower();
    }



    void Update ()
    {
        SetPower();

    }


    void Initialize ()
    {
        powerBar = GameObject.Find ("Power Bar").GetComponent<Slider>();
        myBody = GetComponent<Rigidbody> ();
        anim = GetComponent<Animator> ();

        //Getting the gameobject and particlesystem for dust on ground after character landing
        GameObject dustObject = Instantiate(dustPuff, transform.position, transform.rotation) as GameObject;
        dustParticle = dustObject.GetComponent<ParticleSystem>();

        // Setting the particle dust to disabled on start
        var emission = dustParticle.emission;
        emission.enabled = false;


        powerBar.minValue = 0f;
        powerBar.maxValue = 10f;
        powerBar.value = powerBarValue;
    }



    void MakeInstance ()
    {
        if(instance == null)
            instance = this;


    }


    void SetPower ()
    {
        if (setPower) {
            forceX += tresholdX * Time.deltaTime;
            forceY += tresholdY * Time.deltaTime;

            //if(forceX > 6.5f)
                //forceX = 6.5f;

            //if(forceY > 13.5f)
                //forceY = 13.5f;


            if(forceX > 6.5f)
                forceX = 6.5f;

            if(forceY > 13.5f)
                forceY = 13.5f;


            powerBarValue += powerBarTreshold * Time.deltaTime;
            powerBar.value = powerBarValue;
        }
    }

    public void SetPower (bool setPower)
    {
        this.setPower = setPower;

        if (!setPower) {
            Jump();
        }

    }


    void Jump ()
    {

        if (isGrounded)
        {

            myBody.velocity = new Vector3 (forceX, forceY);
        forceX = forceY = 0f;
        didJump = true;

        anim.SetBool("Jump", didJump);

        powerBarValue = 0f;
        powerBar.value = powerBarValue;
    }

    }


    void OnTriggerEnter (Collider target)
    {

        if (didJump) {

            didJump = false;
            anim.SetBool("Jump", didJump);

            if (target.tag == "Platform") {

                isGrounded = true;


                if (GameManager.instance != null) {
                    GameManager.instance.CreateNewPlatformAndLerp (target.transform.position.x);
                }


                if (ScoreManager.instance != null /*&& forceX > 0.1f && forceY > 0.1f*/) {
                
                    ScoreManager.instance.IncrementScore();
                    ScoreManager.instance.HighScore ();
                }



            }
        }


        if (target.tag == "Dead") {
            if (GameOverManager.instance != null) {
                GameOverManager.instance.GameOverShowpanel();
            }

            Destroy(gameObject);
        }

    }

    private void OnTriggerExit(Collider target)
    {
        if (target.tag == "Platform")
            isGrounded = false;

         
    }
}

Anyone?

Got it working as simple as declaring the following variables at top and some code in whatever function you would like to call the particles in:

    public GameObject dustPuff;
    private ParticleSystem dustParticle;


void WhateverFunction () {

                GameObject dustObject = Instantiate(dustPuff, this.transform.position, this.transform.rotation) as GameObject;
                dustParticle = dustObject.GetComponent<ParticleSystem>();
}
3 Likes

You are strong!)