0

I am trying attach a particle system's gravity modifier with a slider but it is not visible. Here is a screen shot of what I see.

enter image description here

Should I give this up and try to attach the value using a script instead ? ? ?

wewelo welo
  • 99
  • 1
  • 6

2 Answers2

1

You cannot see it from the Editor because main.gravityModifier is a type of MinMaxCurve not float so you cannot assign the Slider value which is float to it from the Editor. There is a function that let's you do that from scripting. That's why it works from script.

Do that from code. It looks like you already got an answer but there are so many things to improve.

1.Don't use GameObject.Find every frame.

You are currently doing this 2 times in a frame with GameObject.Find("Slider") and GameObject.Find("Particle System"). Cache the Slider and the ParticleSystem components. Move those functions to the Start or Awake function.

2.Do not use the Slider value to modify main.gravityModifier every frame.

Use the onValueChanged event to subscribe to the Slider so that you will only modify main.gravityModifier when the slider value changes. The Update function is no longer needed when you do this.

private ParticleSystem ps;
public Slider slider;

// Use this for initialization
void Start()
{
    //Cache the Slider and the ParticleSystem variables
    slider = GameObject.Find("Slider").GetComponent<Slider>();
    ps = GameObject.Find("Particle System").GetComponent<ParticleSystem>();
}

void OnEnable()
{
    //Subscribe to the Slider Click event
    slider.onValueChanged.AddListener(delegate { sliderCallBack(slider.value); });
}

//Will be called when Slider changes
void sliderCallBack(float value)
{
    Debug.Log("Slider Changed: " + value);

    var main = ps.main;
    main.gravityModifier = value;
}

void OnDisable()
{
    //Un-Subscribe To Slider Event
    slider.onValueChanged.RemoveAllListeners();
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Just as a side-note, I would not use `GameObject.Find` unless the objects have truly unique names, to avoid messing up which object you retrieve. – Ian H. Dec 24 '17 at 14:02
  • 1
    Yes. If you have multiple objects with the-same name but under different objects then use the "/" to access them like "parentname/childname". Just don't have multiple **root** object with the-same name. – Programmer Dec 24 '17 at 14:05
  • Thank you! This is highly useful. – wewelo welo Dec 24 '17 at 16:35
  • You are welcome. Don't forget to accept answer if you find it helpful – Programmer Dec 24 '17 at 16:39
0

I got the scripting answer, but if any of you has a better solution you are welcome to share it.

 using UnityEngine;
    using UnityEngine.UI;
    public class SliderScript : MonoBehaviour {

    private double sf;
    private ParticleSystem ps;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        sf = GameObject.Find("Slider").GetComponent<Slider>().value;
        ps = GameObject.Find("Particle System").GetComponent<ParticleSystem>();
        var main = ps.main;
        main.gravityModifier = (float)sf;
        print(sf );
    }
}
wewelo welo
  • 99
  • 1
  • 6