Search Unity

Audio Cannot play a Disabled audio Source.

Discussion in 'Audio & Video' started by Shanrakk, Apr 26, 2017.

  1. Shanrakk

    Shanrakk

    Joined:
    Apr 26, 2017
    Posts:
    2
    Except that... The source *isnt* Disabled. I've validated the source component and the object with the script attached both have valid, working audio source components. By themselves I can play the audio component fine, so its not an issue there. I've checked to make sure that the game object plays the audio clip before the object is destroyed. I've actually attempted to place the sound directly into void update to ensure that it is calling properly and still returns this error. If I press "play on wake" my audio will work once, the first time the prefab is spawned, and then die. I've paused during this point to determine whether or not the cloned object in question is getting the audiosource and audioclip properly- it is. I've utilized Play, PlayClipAtPoint, and PlayOneShot- none of these resolve my issue.

    Can I find some help on this? I've been googling it for ages with no response that has benefited me :( Here is an example of my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DestrcutionScript : MonoBehaviour {
    6.  
    7.     private GameObject Circle;
    8.     public AudioClip PopSound;
    9.     public AudioSource source;
    10.  
    11.     void Start() {
    12.         AudioSource source = GetComponent<AudioSource>();
    13.     }
    14.  
    15.     void OnTriggerEnter2D() {
    16.         source.PlayOneShot(PopSound);
    17.         Debug.Log(gameObject);
    18.  
    19.         Object.Destroy(Circle);
    20.  
    21.         Debug.Log("death");
    22.     }
    23. }
    24.  
    Thank you guys so much!
     
  2. lukitone

    lukitone

    Joined:
    Dec 25, 2011
    Posts:
    49
    the problem is that you destroy your sound before you hear it. don't destroy your audio before it has been completly played. you can add in a coroutine which is checking if the sound is still playing and start it after you are playing the sound instead of destroying. In the coroutine:

    Code (CSharp):
    1.  
    2. if(!source.isplaying){
    3. Object.Destroy(Circle);
    4. }
     
    GoLuckyMe and Shanrakk like this.
  3. Shanrakk

    Shanrakk

    Joined:
    Apr 26, 2017
    Posts:
    2
    Thanks @lukasgmusic . I discovered that the audio file was playing at the same time as the object being destroyed so it didnt play. I ended up just briefly hiding the object for the duration of the audio file, then letting the object be destroyed afterword.
     
    Darkgaze likes this.
  4. Deleted User

    Deleted User

    Guest

    How did you do that same issue here.
     
  5. Nissepadden

    Nissepadden

    Joined:
    Oct 26, 2020
    Posts:
    1

    Hi. Try this

    using UnityEngine;
    using UnityEngine.UI;
    [RequireComponent(typeof(Button))]

    public class ClickSound : MonoBehaviour
    {
    public AudioClip sound;
    private Button button { get { return GetComponent<Button>(); } }
    private static AudioSource soundPlay;

    void Start ()
    {

    button.onClick.AddListener(() => PlaySound());

    }
    void PlaySound()
    {
    soundPlay = Instantiate(gameObject.AddComponent<AudioSource>());
    soundPlay.clip = sound;
    soundPlay.playOnAwake = false;
    soundPlay.PlayOneShot(sound);
    DestroyObject(soundPlay, 1f);
    }
    }
     
  6. rameezfyyz

    rameezfyyz

    Joined:
    May 10, 2020
    Posts:
    5
    Just add your PREFAB or OTHER audio source to the ACTIVE SCENE (preferrably in an empty object because there are many sound effects in a single game). The problem should solve this way. In above example you have to instantiate and destroy the audio source everytime you want to play it. I don't think that's a good approach.
     
    Acarius, HallamAkbar and Frazzels312 like this.