Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Problem with Audiosource not playing.

Discussion in 'Scripting' started by Bdelcast, Jul 9, 2019.

  1. Bdelcast

    Bdelcast

    Joined:
    Jul 11, 2014
    Posts:
    11
    *EDIT: Now I realize that this question may be better asked in a different forum. Sorry if I posted it in the wrong place. I'm mostly a programmer so it was my knee jerk reaction.

    So, I have a situation that is really frustrating me. I have used this same method several times, it's not a complex setup, but it simply won't work in this specific situation, and I can't pinpoint the reason.

    I have a prefab of a gate, it has an audio source in a disabled game object.This audio source game object is enabled in an animation when the player walks close to the gate.
    the object is set up like this:
    FIG1.jpg
    The system works fine: when the character walks to the door it plays the animation, and it opens or closes the gate accordingly. HOWEVER, the audio will NOT play, even though the audiosource is set as "play on awake" and the gameobject IS being enabled in the editor.

    the audio in the prefab is set up like this: FIG2.jpg
    I have tried with an intermediary script triggered from an animation event as well but although it is also triggered, the audio source will not play, no matter what.

    This is the script that triggers the sound, and the animation DOES trigger PlayAudioSource(string name):

    Code (CSharp):
    1. public class PlaySoundRemote : MonoBehaviour {
    2.     public AudioSource[] sources;
    3.     public float fadeoutTime;
    4.  
    5.     public void PlayAudioSource(string name)
    6.     {
    7.         AudioSource src = FindClip(name);
    8.         if (src)
    9.         {
    10.             src.Stop();
    11.             src.Play();
    12.         }
    13.     }
    14.  
    15.     public void PlayRandomizedSource(string name)
    16.     {
    17.         AudioSource src = FindClip(name);
    18.  
    19.         int rdm = Random.Range(0, 2);
    20.  
    21.         if (src && rdm == 0)
    22.         {
    23.             src.Stop();
    24.             src.Play();
    25.         }
    26.     }
    27.  
    28.  
    29.     public void StopAudioSource(string name)
    30.     {
    31.         if (fadeoutTime == 0)
    32.         {
    33.             fadeoutTime = 0.001f;
    34.         }
    35.         AudioSource src = FindClip(name);
    36.         if (src)
    37.         {
    38.             StartCoroutine(FadeOutSource(src));
    39.         }
    40.     }
    41.  
    42.     public void SetFadeOutTime(float time)
    43.     {
    44.         fadeoutTime = time;
    45.     }
    46.  
    47.     private IEnumerator FadeOutSource( AudioSource source)
    48.     {
    49.         float sourceOriginalVolume = source.volume;
    50.         float deltaVolume = sourceOriginalVolume / fadeoutTime;
    51.         while (source.volume > 0)
    52.         {
    53.             source.volume -= deltaVolume * Time.deltaTime;
    54.             yield return null;
    55.         }
    56.         source.volume = sourceOriginalVolume;
    57.         source.Stop();
    58.     }
    59.  
    60.     private AudioSource FindClip(string name)
    61.     {
    62.         for (int i = 0; i < sources.Length; i++)
    63.         {
    64.             if (sources[i].clip.name == name)
    65.             {
    66.                 return sources[i];
    67.             }
    68.         }
    69.         return null;
    70.     }
    71. }
    I am using this in several other places, and it works fine, but not with these audiosources.

    I found out that if I do set the audiosource to "LOOP" as shown on the image above, the audio DOES play, but I don't want this sound to be looping, and it is a short sound so it's difficult to clamp it with the animation.

    Is this a bug? does anyone have any idea as to why it could be failing?
    Thanks.
     
    Last edited: Jul 9, 2019