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.
Should I give this up and try to attach the value using a script instead ? ? ?
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.
Should I give this up and try to attach the value using a script instead ? ? ?
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();
}
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 );
}
}