- Home /
how to emit particle system attached to object in code?
Hi, i am trying to make a particlesystem(fire) attached to a campfire to be emitted(or change its color at least) when i throw the twig to the campfire, the count is used to count how many twigs are thrown to the campfire,everthing works except the change of color of the particle system fire
public class count : MonoBehaviour { public int counter; public Text text; public Rigidbody fireball; private ParticleSystem fire;
// Use this for initialization
void Start () {
counter = 0;
text.text = "count: " + counter.ToString();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if(other.CompareTag("twig"))
{
this.GetComponent<ParticleSystem>().startColor = new Color(1, 0, 1, .5f);
other.gameObject.SetActive(false);
counter++;
text.text = "count: " + counter.ToString();
}
}
}
Answer by ifurkend · Apr 28, 2018 at 03:55 PM
startColor belongs to the “main module” struct of particle system, so you need to give your particlesystem a var, declare the module of the ps and finally access its properties. You can read the official blog for more details.
In fact, it’s more performance sound to GetComponent/declare your ps and ps module variables on Awake() or OnEnable() instead of Update() on the fly.
Your answer
Follow this Question
Related Questions
ParticleSystem Random Between Two Colors 4 Answers
ParticleSystem.emission randomly wont reset 1 Answer
How to use emission.enable correctly? 1 Answer
Particle System using distance emission stops emitting when script active.. 0 Answers
How do you change the colour of individual particles having assigned them a material? 1 Answer