How Audio Source is different from particles

My question when we use audio source we have to cache and declare before we use like this

[SerializeField] ParticleSystem crashParticle;

ParticleSystem PS;

private void Start()
{

    AS = GetComponent<AudioSource>();
  
}

AS.Stop(); or

AS.PlayOneShot(crashingSound);

But in case of particle system we are staright saying

winParticle.Play();

we not caching or it declaring it so how it is working

1 Like

Hi Blaze,

winParticle.Play(); works only if a) the winParticle variable was declared, and b) an object was assigned to winParticle.

I get it in order to play it we have to give some data my real question was why we are not caching it like we did with Rigidbody or audio sourse and directly playing with variable

What is “it”?

It is particle system

We do cache the ParticleSystem object. However, since it is not attached to the same game object as AudioSource, we cannot call GetComponent<ParticleSystem>(); to get the reference to that object. Instead, we assign the reference in the Inspector. We could do the same with the AudioSource object.

I still don’t understand this. I did the same thing as this user and tried to define the variable for particleSystem and do GetComponent in the same way i did with audio. When do you use GetComponent vs just using the method that comes with Unity? It seems like the audio and particle should be the same process but they are different.

Hi @jellyBones,

What exactly did you do? Did you test your idea? Did it work? Both approaches should work given your code knows the game object on which you call GetComponent. If your script is attached GameObjectA, the AudioSource component is attached to GameObjectA and the ParticleSystem component is attached to GameObjectB, you cannot get both the AudioSource and the ParticleSystem with GetComponent because they are not on the same game object as your script. You need a reference (“link”) to the other game object first.

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

Privacy & Terms