- Home /
Question by
aqsanadeem82 · Nov 23, 2020 at 05:17 AM ·
unity 5particlesparticlesystemparticle systemparticleeffect
How to prevent particle system from restarting when value changes from script?
Whenever I change my particle system values from the script during runtime the particle system starts from scratch but I don't want it to restart I want it to continue from where its values are changed.
Comment
$$anonymous$$ultiple values, such as speed, render mode, color over a lifetime, rate over time, the trail here is what I am doing, I am moving still stars via code on button click the start moving with the trail, I want the still stars to move not to disappear, here is what I am doing. Link
Answer by ifurkend · Nov 23, 2020 at 10:06 AM
You cannot modify the "System" and expect it to affect the "Particles" which have already been emitted. Instead you need to get and set each particle individually in a loop:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveParticle : MonoBehaviour
{
public ParticleSystem _ps;
ParticleSystem.MainModule _ps_main;
ParticleSystem.Particle[] _p;
public float _speed = 5f;
int numParticlesAlive;
void Awake()
{
_ps = this.GetComponent<ParticleSystem>();
_ps_main = _ps.main;
_p = new ParticleSystem.Particle[_ps_main.maxParticles];
}
public void StartParticle()
{
numParticlesAlive = _ps.GetParticles(_p);
for (int i = 0; i < numParticlesAlive; i++)
{
_p[i].velocity = new Vector3(0f, 0f, _speed);
}
_ps.SetParticles(_p, numParticlesAlive);
}
public void ResetParticle()
{
_ps.Stop();
_ps.Play();
}
}