Tried attaching the particle effect named Fall_Particleeffect_Leaves_01 to a slider script that changes the emission, but I struggle with it

Viewed 31

So the original idea was to attach the particle effect emission to the SeasonIntensitySlider, so that when the slider value is changed, the emission value is changed.

I wasn't able to get that done, so now I tried using the same input that changes the slider value to also change the emission value at the same time - which didn't work either (and was a band-aid fix as well). And I think I still got the wrong code for what I'm trying in the first place.

I attached the code below. Can anyone tell me how I can link them?

using System.Collections;
using UnityEngine.UI;

public class SeasonIntensity : MonoBehaviour
{
    public Slider SeasonIntensitySlider;

    public void Start()
    {
        SeasonIntensitySlider.onValueChanged.AddListener(delegate { ValueChangeCheck(); }); //Adds a listener to the main slider and invokes a method when the value changes.
    }

    public void ValueChangeCheck()
    {
        Debug.Log(SeasonIntensitySlider.value); // Invoked when the value of the slider changes.
    }

    public void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") > 0f) //Foward
        {
            SeasonIntensitySlider.value += 0.1f;
        }

        if (Input.GetAxis("Mouse ScrollWheel") < 0f) //Backwards
        {
            SeasonIntensitySlider.value -= 0.1f;
        }

        if (Input.GetAxis("Mouse ScrollWheel") > 0f) //Foward
        {
            ParticleSystem ps = GetComponent<ParticleSystem>();
            var em = ps.emission;
            em.enabled = true;

            em.rateOverTime += 5f;
        }

        if (Input.GetAxis("Mouse ScrollWheel") < 0f) //Backwards
        {
            ParticleSystem ps = GetComponent<ParticleSystem>();
            var em = ps.emission;
            em.enabled = true;

            em.rateOverTime -= 5f;
        }
    }
}
1 Answers

First, i recommend to you to clean up a little your code. Put the particle system reference on top. Then assign it all from start. You have not to declare so many variables (because it's in update and every frame new unnecessary variable is created. It consumes fps). Then make only 2 if statements instead of 4, you can fit this in only 2 if's.

And for the problem, maybe check if you've assigned the slider variable and i know different method of adding listener. AddListener(() => ValueChangeCheck());

using System.Collections;
using UnityEngine.UI;

public class SeasonIntensity : MonoBehaviour
{
    public Slider SeasonIntensitySlider;
    private var em;

    public void Start()
    {
        SeasonIntensitySlider.onValueChanged.AddListener(() => ValueChangeCheck()); //Adds a listener to the main slider and invokes a method when the value changes.

        em = GetComponnent<ParticleEffect>().emission;
    }

    public void ValueChangeCheck()
    {
         Debug.Log(SeasonIntensitySlider.value); // Invoked when the value of the slider changes.
    }
    public void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") > 0f) //Foward
        {
            SeasonIntensitySlider.value += 0.1f;

            em.enabled = true;
            em.rateOverTime += 5;
        }

        if (Input.GetAxis("Mouse ScrollWheel") < 0f) //Backwards
        {
            SeasonIntensitySlider.value -= 0.1f;

            em.enabled = true;
            em.rateOverTime -= 5;
        }
    }
}
Related