Confusion about AudioSource and caching?

Hi Killerbanana,

Welcome to our community! :slight_smile:

It seems AudioClip and AudioSource are types of variable.

Yes, they are types.

However, shouldn’t variables begin with a lowercase letter (camel casing)?

Yes, the names of variables usually start with a minuscule letter. However, that’s just a naming convention. If you introduced a name, you have to use that name “as it is” because C# is case-sensitive.

[SerializeField] audioClip audioClip is invalid because AudioClip is the type, and it is spelt AudioClip, not audioClip. The variable name audioClip is fine because it is something that you introduce here. The AudioClip type/class already exists.

AudioSource.PlayOneShot(audioClip); would mean that you call this method on the class. The method requires an instance/object, though. For this reason, you need a variable. For example:

AudioSource abc = GetComponent<AudioSource>();
abc.PlayOneShot(audioClip);

I named the variable abc on purpose, so it is obvious what the variable name is. Of course, I could have named my variable audioSource.

Did this clear it up for you?


See also: