Hi, I want to play the Particle system (with children ) just once, when the progress bar reaches 100%. When I use Play method under the Update function, it works, but it loops (I know this is not the right way) . Since I am new to C#, please help me with the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ProgressBarParticleController : MonoBehaviour {
public Transform Progress_Bar;
public ParticleSystem particleBurst;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;
void Update () {
if (currentAmount < 100) {
currentAmount += speed * Time.deltaTime;
}
// With this statement below the Particle System bursts at the end, but it loops even though Looping is unchecked in the editor
else {
particleBurst.Play();
}
Progress_Bar.GetComponent<Image> ().fillAmount = currentAmount / 100;
}
/*
// With this statement below nothing happens.
/* public void ParticleBurst () {
if (currentAmount == 100) {
particleBurst.Play ();
// particleBurst.Emit (30);
}
}
*/
}
Hey,
you can use particleBurst.Emit();
instead of particleBurst.play();
and it should only play once.
You can also use Play by unchecking the Looping option in the inspector and also set the duration to the number of seconds you want the particle to play. (by default it is 5)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ProgressBarParticleController : MonoBehaviour {
public Transform Progress_Bar;
public ParticleSystem particleBurst;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;
void Update () {
if (currentAmount < 100) {
currentAmount += speed * Time.deltaTime;
}
// With this statement below the Particle System bursts at the end, but it loops even though Looping is unchecked in the editor
else {
particleBurst.Emit(100);
}
Progress_Bar.GetComponent<Image> ().fillAmount = currentAmount / 100;
}
}
@Sugavanas thanks for the answer, but I have tried this before and it didn’t work. In the meantime I have found the solution. This code works in my case…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ProgressBarParticleBurst : MonoBehaviour {
public Transform Progress_Bar;
public ParticleSystem particleBurst;
private bool played;
[SerializeField] private float currentAmount;
[SerializeField] private float speed;
void Update () {
if (currentAmount < 100) {
currentAmount += speed * Time.deltaTime;
}
else
if (!played) {
particleBurst.Play ();
played = true;
}
Progress_Bar.GetComponent<Image> ().fillAmount = currentAmount / 100;
}
}