Both particles are playing/triggering at the same time (even out of code)

Unsure as to why but the explosion and success particles are playing on top of each other on every play. This is true outside of code, so I don’t believe it is code related.

I dragged both particles as children of my rocket and in the inspector, dragged in the particles from the hierarchy and not the prefabs. Where am I going wrong here?

image

public class CollisionHandler : MonoBehaviour
{
    [SerializeField] float delay;
    [SerializeField] AudioClip crashSound;
    [SerializeField] AudioClip winSound;
    [SerializeField] ParticleSystem explosion;
    [SerializeField] ParticleSystem success;

    Movement movement;
    AudioSource audioSource;

    bool playAudio;

    private void Start()
    {
        audioSource = GetComponent<AudioSource>();
        explosion = GetComponent<ParticleSystem>();
        success = GetComponent<ParticleSystem>();

        playAudio = true;
    }

    void OnCollisionEnter(Collision collision)
    {
        delay = 2f;

        switch(collision.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("Friendly");
                break;
            case "Fuel":
                Debug.Log("Fuel");
                break;
            case "Finish":
                Debug.Log("You win");
                StartCoroutine(StartNextLevelSequence());
                break;
            default:
                Debug.Log("You blew up");
                StartCoroutine(StartCrashSequence());
                break;
        }

        void ReloadLevel()
        {
            
            int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
            SceneManager.LoadScene(currentSceneIndex);
        }
        void LoadNextLevel()
        {
            int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;

            if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
            {
                nextSceneIndex = 0;
            }    

            SceneManager.LoadScene(nextSceneIndex);
        }


        IEnumerator StartCrashSequence()
        {

            explosion.Play();
            PlayAudio(crashSound);
            
            movement = GetComponent<Movement>();
            movement.enabled = false;
            yield return new WaitForSeconds(delay);
            ReloadLevel();

        }

        IEnumerator StartNextLevelSequence()
        {
            //plays the Win Sound and Success Particles
            success.Play();
            PlayAudio(winSound);

            //turns off player controls on impact
            movement = GetComponent<Movement>();
            movement.enabled = false;

            //waits X seconds then reloads level
            yield return new WaitForSeconds(delay);
            LoadNextLevel();

        }

        void PlayAudio(AudioClip audio)
        {
            if (playAudio) //evaluates boolean of playAudio (default true) then plays the audio and disables play audio to prevent stacking
            {
                audioSource.Stop();
                audioSource.PlayOneShot(audio);
                playAudio = false;
            }
        }
    }
}

Going to answer my own question and leave this here in case anyone else goofed the same way I did.

This was a code problem. These two GetComponents were the source of the problem as they were unnecessary. The component was the child and not necessary on the parent rocket which was playing all associated children by default.

        explosion = GetComponent<ParticleSystem>();
        success = GetComponent<ParticleSystem>();
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms