- Home /
Bool Value reversed
Hey all,
Got a situation here, wondering if I can get some help. Have a game object emitting particles based on a bool. It should "Play()" when value is true. Problem is, it is doing just the opposite, plays when value is false, and stops when value is true.
public class Flamethrower : MonoBehaviour
{
public float lvl, damage;
public bool fire;
public ParticleSystem flame;
void Start ()
{
}
void OnParticleCollision (GameObject other)
{
Armor armor = other.GetComponent<Armor> ();
if (armor != null)
armor.TakeDamage (damage);
}
void Update ()
{
Debug.Log (fire);
if (fire == true) {
flame.Play ();
}
}
}
even more weird is that i am not calling a Stop(), but it stops on its own as soon as the value is true. What is going on here?
Answer by hrishihawk · Feb 19, 2018 at 06:07 AM
Problem with your script is that you are calling the Play method even when the particle effect is not finished playing.Which makes the particle effect to play it's first frame on every frame.You can avoid this by using one more condition check as below
if (fire == true && !flame.isPlaying) {
flame.Play ();
}
Thanks, feel like I should have realized this. This is what I ended up with
if (fire == true && !flame.isPlaying) {
flame.Play ();
} else if (fire == false && flame.isPlaying) {
flame.Stop ();
works great. Thanks again!
Answer by NorthStar79 · Feb 19, 2018 at 05:23 AM
Hello, you are calling Play() every frame when fire == true , this causes it restart every frame. this is your problem.