My particlesystem.Play()
does not work. I have spent few hours on this issue and still could not figure out.
Whenever my character hits an object, it will call the function particleAuraPlay()
, and the log message "HIT" is showing which means the function is being called properly.
When the playAura
is set to true, the particle should play, and again the log message "Running" is showing up, too. Since the message is showing I assume my logic is correct but the particle just won't start playing. Can anyone please solve my problem?
using UnityEngine;
using System.Collections;
public class ParticleController : MonoBehaviour {
private bool playAura = false;
private ParticleSystem particleObject;
void Start () {
particleObject = GetComponent<ParticleSystem>();
particleObject.Stop();
}
void Update () {
if (playAura)
{
Debug.Log("Running");
particleObject.Play();
}
}
public void particleAuraPlay()
{
Debug.Log("HIT");
playAura = true;
}
}
When the playAura is set to true, the particle should play, and again the log message "Running" is showing up, too. Since the message is showing I assume my logic is correct but the particle just won't start playing
The is a logic error.
When the particleAuraPlay()
function is called, playAura
is set to true
.
In the Update functions,particleObject.Play();
will be called every frame since playAura
is true
.
You can't do this. Calling particleObject.Play();
every frame will not actually do anything as it will attempt to play and stop particles in each frame resulting to no particles at-all.
The solution is to check if playAura
is true
, if true
, call the particleObject.Play();
function then set playAura
to false
so that the particleObject.Play();
function won't be called again until particleAuraPlay()
is called again.
The new Update()
function fixes the logic error:
void Update()
{
if (playAura)
{
Debug.Log("Running");
particleObject.Play();
playAura = false;
}
}
In your particleAuraPlay() function you just need to check if your ParicleSystem is running or not you can achieve this by this code...
public void particleAuraPlay(){
if(!particleObject.isPlaying){
particleObject.Play();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With