What obsolete writes, it most likely swears at audio . Such
Component.audio 'is obsolete: `Property audio has been deprecated. Use GetComponent () instead.
The warning is written specifically what to do. Instead of audio use GetComponent<AudioSource>() . ( Always read the warnings and at least translate to Google. In the messages, they specifically ALWAYS say what to do. And they don't send in three letters! )
obsolete is outdated. That is, so now it is better not to do. Previously, it was possible to refer to a component by its name, without declaring and initializing it in the script. Now it is necessary to declare and initialize. How?
As briefly as possible:
On the object with the script should be component AudioSource
To play a sound on it once, you must first access (initialize) it via GetComponent<AudioSource>() , as described above, and then just call the PlayOneShot method, which takes two parameters as input: sound itself and volume (not necessarily ). Everything! Happy end.
Example:
[RequireComponent(typeof(AudioSource))] public class ExampleClass : MonoBehaviour { public AudioClip impact; // Π½Π°Ρ Π·Π²ΡΠΊ AudioSource audio; // ΠΎΠ±ΡΡΠ²Π»ΡΠ΅ΠΌ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ Π°ΡΠ΄ΠΈΠΎ ΠΈΡΡΠΎΡΠ½ΠΈΠΊΠ° void Start() { // ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΡΠ΅ΠΌ ΠΈΡΡΠΎΡΠ½ΠΈΠΊ ΠΈΠ·Π΄Π°Π²Π°Π½ΠΈΡ Π·Π²ΡΠΊΠΎΠ² audio = GetComponent<AudioSource>(); // ΠΠΎΡΠΏΡΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΠΌ audio.PlayOneShot(impact, 0.7F); } /* ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΈ ΡΠ°ΠΊ void OnCollisionEnter() { audio.PlayOneShot(impact, 0.7F); } */ }
The example is taken from official docks.
You can directly throw the played file into the AudioSource component in the AudioClip field

and call the method immediately on it:
AudioSource audio; // ΠΎΠ±ΡΡΠ²Π»ΡΠ΅ΠΌ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ Π°ΡΠ΄ΠΈΠΎ ΠΈΡΡΠΎΡΠ½ΠΈΠΊΠ° void Start() { // ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΡΠ΅ΠΌ ΠΈΡΡΠΎΡΠ½ΠΈΠΊ ΠΈΠ·Π΄Π°Π²Π°Π½ΠΈΡ Π·Π²ΡΠΊΠΎΠ² audio = GetComponent<AudioSource>(); // ΠΠΎΡΠΏΡΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΠΌ audio.PlayOneShot(audio.clip); }
or even like this:
public AudioClip impact; void Start() { GetComponent<AudioSource>().PlayOneShot(impact, 0.7F); }
Because if you twist something in the Update method, which is called every frame, then you need to make sure that the method is invoked once, for example, using flags to signal, such as isPlayed or not. Otherwise, each frame will access this method clearly more than once.