Im wondering why this code does not work

i’ve been trying to replicate the code which play sound into the particle system.

like this:
parameters
[serializedfield] audioclip finishAudio;
[serializedfield] particlesystem finishParticle

cache
audiosource aSource;
particlesystem pSystem;

void start{
aSource = getcomponent():
pSystem = getcompoenent()

}

oncollisionenter

case “finish”

playAudio();

playAudio(){

aSource.play(FinishAudio);
pSystem.play(finishParticle);
}

Why do we instead just call for the Parameter variable(finishParticle.Play()) instead of getting the component first and parsing it through the Parentheses like the audioclip?

Hi,

What do you mean by “does not work”? What did you expect to happen? What happened instead?


See also:

what i meant was, why my approach to code a particle system was not working.

i will share my code and put ** in between the line of code that i am trying to put in practice.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour
{
    [SerializeField] float loadDelayC;
    [SerializeField] float loadDelayF;
    [SerializeField] AudioClip soundEngineCrashing;
    [SerializeField] AudioClip soundEngineFinish;
    [SerializeField] ParticleSystem particleCrashing;
    [SerializeField] ParticleSystem particleFinish;

    AudioSource aSource;
    **particlesystem pSystem;**

    

    bool isTransitioning;
    void Start()
    {
        aSource = GetComponent<AudioSource>();
        **pSystem  = GetComponent<ParticleSystem();**
       

    }
    void OnCollisionEnter(Collision collision)
    {
        if (isTransitioning) { return; }
        switch (collision.gameObject.tag)
        {
            case "Finish":

                NextSceneDelay();
                break;

            default:

                startCrashSequence(); 
                
                break;
        }
    }

    void startCrashSequence()
    {
        
        isTransitioning = true;
        
        aSource.Stop();
        aSource.PlayOneShot(soundEngineCrashing);
        **pSystem.Play(particleCrashing);**
        GetComponent<Movement>().enabled = false;
        Invoke("ReloadScene", loadDelayC);
    }

    void NextSceneDelay()
    {
        isTransitioning = true;
        **pSystem.Play(particleFinish);**
        aSource.Stop();
        aSource.PlayOneShot(soundEngineFinish);
        GetComponent<Movement>().enabled = false;
        Invoke("NextScene", loadDelayF);
    }
    void NextScene()
    {
        int currentSceneNumber = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneNumber + 1);
        int nextSceneIndex = currentSceneNumber +1;
        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
            {
            nextSceneIndex = 0;
        }
        SceneManager.LoadScene (nextSceneIndex);
             
    }
    void ReloadScene()
    {
        int currentSceneNumber = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneNumber );
    }
}

so i’ve been trying to call the particle system the same way as i would call audio source

i was expecting it to work and play the particle effects i have set up in my scene

i know the correct way is to call it like this:
particleFinish.play();
particleCrash.play();

the question is why did the code that i was trying not work?

forgot to @
please review the above message.

I’m not sure there’s a way to answer that. It’s just not the way that class was programmed. When you create a class and make methods for it, you can decide what sort of parameters the method takes in.

So someone could have made it so the Play method works that way by making a method that looks like this:

static void Play (Particle system pSys) {
pSys.Play();
}

I’m not sure if you can have a static and non-stick with the same name or not. Probably not.

But they did not. Instead, Play() is a method on the particle system instance itself that gets it going. You could always make a method like the above and put it on one of your scripts. But since you already have a reference to it, what would be the point?

I suppose you could add code that does other stuff like check if the player is close by or something, deciding whether or not to play based on that condition.

It may be worth noting that audiosource also work this way too and has a Play() method that is run from the instance that will play the clip that has been previously loaded into it (usually setup in the Unity inspector).

1 Like

Many thanks!

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

Privacy & Terms