I try to play the sound through audio.PlayOneShot, Unity emphasizes me, writes something about obsolete. As briefly as possible, how to play MP3 once? There are MP3s in Assets, there is a script

using UnityEngine; using System.Collections; public class taptoscream : MonoBehaviour { public float gts = 0; float nosik = Scream.nos; public AudioClip MyAudio; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if(gts == nosik) { dog(); } if(gts > nosik) { dog(); } } void dog() { audio.PlayOneShot(MyAudio); } } 

Here the last line underlines red. How to avoid and what to change? Just play the sound once.

  • And you write what he writes) - Alexey Shimansky

1 answer 1

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

enter image description here

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.