Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Particle System not playing within If Statement

Discussion in 'Scripting' started by superdog2010, Apr 23, 2021.

  1. superdog2010

    superdog2010

    Joined:
    Aug 17, 2020
    Posts:
    4
    Hello.

    I am trying to get a water particle system to come out of a faucet when I turn one of the handles (used with a hinge joint).

    I check for the angle of the faucet, and successfully output the correct message to the console, so I know the if statement if working on it's own, however, "water.Play()" does not seem to start the particle animation.

    Just for the record, the faucet handles are not children of the faucet object, but the water particle system is.
    Also, I tried unchecking "looping" for the particle system, which did not fix it.

    As commented out in my code below, I also tried "water.enableEmission = true/false", which also did not work.

    Should I be using another function to play the system, such as "instantiate"?

    Thank you, and here is the simple code I am using:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FaucetHandle : MonoBehaviour
    6. {
    7.  
    8.     public GameObject handle;
    9.     public ParticleSystem water;
    10.  
    11.     void Update()
    12.     {
    13.         if(handle.transform.localRotation.eulerAngles.y > 190)
    14.         {
    15.             water.Play();
    16.             //water.enableEmission = true;
    17.             Debug.Log("Water playing. Rotation: " + handle.transform.localRotation.eulerAngles.y);
    18.         }
    19.         else
    20.         {
    21.             water.Stop();
    22.             //water.enableEmission = false;
    23.             Debug.Log("Water stopped. Rotation: " + handle.transform.localRotation.eulerAngles.y);
    24.         }
    25.     }
    26. }
    27.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Normally you'd want to call Play() once to start it playing. You're calling it over and over every frame. I haven't tried that, so not sure if that causes it to restart each time.
     
    superdog2010 likes this.
  3. superdog2010

    superdog2010

    Joined:
    Aug 17, 2020
    Posts:
    4
    Ah, true. Is there a way to check if the water is playing? Such as "if (water.Play() == true) {} "?
     
  4. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
  5. superdog2010

    superdog2010

    Joined:
    Aug 17, 2020
    Posts:
    4
    Awesome, got it working by just checking if the water.isPlaying.
     
    Joe-Censored likes this.