C# Script Question



Hi, i am curious on the following code:
audioSource.Stop(); //if not hitting space, stop audio

we define AudioSource audioSource; in the beginning of the script so that we can use it later on to code for audioclip to play/stop:

public class rocket : MonoBehaviour
    [SerializeField] float rcsThurst = 150f;
    [SerializeField] float mainThurst = 100f;
    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip success;
    [SerializeField] AudioClip death;
    [SerializeField] ParticleSystem mainEngineParticles;
    [SerializeField] ParticleSystem successParticles;
    [SerializeField] ParticleSystem deathParticles;

    Rigidbody rigidBody;
    AudioSource audioSource;

But when we are coding for particle system, why do we not define anything for particle system just like what we did for audio source? I am confuse on the difference of implementation on audio-source and particle system.
If i want to manipulate other component of game object in the future, how to see if we need to define or not?


I’m not sure I understand what you mean.

The particle system seems to be defined here:
ParticleSystem mainEngineParticles;


I think your confusion might stem from this detail: the object in question has an AudioSource component. This allows it to be “assigned” in the Start() method with the GetComponent() call. Meanwhile, for ease of development, an alternative technique is used to assign the component to a variable when it comes to the particle systems; SerializeField allows us to drag-drop a prefab particle system into the object via the inspector. Doing it this way means we don’t need to do it the other way. There’s always more than one way to do something; you need to find the way that works best for you.