Search Unity

Audio Prefabs not playing audio- SOLVED!

Discussion in 'Audio & Video' started by larswik, Jul 30, 2017.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    SOLVED Below.
    I have been slowly working on a game for the past 8 months or so. I updated to the latest 2017 of unity and now I have spent days figuring out why a prefab won't play sound?

    I have the standard code 'public AudioSource audio;' with an AudioSource file connected to that component. Then when my object gets hit, it gets killed off with the Destroy(); function but it never plays the audio.Play(); I got it to work as not a prefab while testing but as soon as I instantiate the object and it gets killed off, no audio.

    I have spent a couple of days looking around this form and searching. Playing audio is not a difficult thing and I have other Game Objects that play audio, but something has changed or it is a bug I am guessing?

    Any thoughts?
     
    Last edited: Aug 7, 2017
  2. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Update: Still not working correctly. As long as it is not a Prefab it works fine. Seems to be OK if it starts off in the Hierarchy and the audio source is still in the hierarchy. But when I drag the game object to make a prefab out of it and the audio source is still in the hierarchy, when the prefab is instantiated it looses it's reference to the audio source.

    I try to relink the audio source in the hierarchy to the prefab but it won't except it in the Audio Source field. I then drag the audio source from the hierarchy to the assets and it creates a prefab from the audio source. Now I can drag and drop the prefab audio source on to the prefab game object and it allows it. Just to be clear, The audio source field that I am dragon it to is the 'script' where I declared a "public AudioSource mySound".

    Now the audio source is still there when I instantiate the object but it does not play anything?

    Funny thing is I have other audio sources that I created playing fine in the game, its just the instated prefabs that are problematic.
     
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Finally Solved myself. If you have tried everything else, this worked for me. It should be a very simple thing to play a sound from your Prefab but I could not get a Prefab to play a sound. It wasn't because the Destroy() function would cut it off, I disabled that and it still would not play.

    Found the answer while I was looking for an explanation as to why I dragged an UI Button from the Hierarchy to make a Prefab object out of it. All of the 'On Click' links would be blank and was unable to link them correctly. I applied that method to the sound and it worked!

    1. The code you probably already have in your script is this to link the audio source to the script.
    Code (CSharp):
    1. public AudioSource audio;
    2. In your prefab GameObject, add an AudioSource component, by clicking the add component.
    3. Now drag what ever audio you want to play from your Assets on to the Audio Clip section of your newly created AudioSource component.
    4. Now here is the step that fixed everything for me. Click and hold on the Audio Source name in your prefab, in the component list, and physical drag and drop that on to the Audio Source for your script, the textfield from step 1 above. (look at the included photo)
    5. Now just use the syntax of what ever your Audio Source is with play(); function, myAudio.Play();

    That fixed the problem for me. But since I was Destroying the game object I had to delay it till the audio finished. I found this code that worked great but it's only needed if your using the Destroy() function after you called the Play() function.

    Code (CSharp):
    1. sound.Play();
    2. transform.position = Vector3.one * 9999f; // move the game object off screen while it finishes it's sound, then destroy it
    3. Destroy(gameObject, sClip.length);
    1. Played the sound.
    2. Moved the game object off the screen
    3. The sClip is a link to the audio clip which I get the length of the clip and use that as the Destroy(); delay/
    That was not my idea but it works great.

    I was creating my Audio Sources in the Hierarchy, then dragging my audio to the audio clip section of it. Then I was dragging the Audio Source from the Hierarchy to the prefab script where I had the public Audio Source audio and dropping it there, that did not work. No where in the searches was it clear to me that I was suppose to drag the Audio Source component from within the prefab to the public Audio Source of the script of the same prefab.

    All the other ways I found worked to play audio, along as it was not an Instantiated Prefab. Hope this helps someone else.
     

    Attached Files:

    • fix.jpg
      fix.jpg
      File size:
      42.5 KB
      Views:
      6,286
    Last edited: Jan 31, 2018
  4. Melozhe

    Melozhe

    Joined:
    Jan 9, 2018
    Posts:
    1
    I have the same problem for days. Now I finally solved it thanks to you. Great work!!!
     
  5. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Good, I am glad I posted the answer since I struggled with it for days too. It is such a simple fix but not obvious one and I found no other sources offering this answer that I could find.
     
  6. gamesbynilo

    gamesbynilo

    Joined:
    Feb 17, 2018
    Posts:
    1
    Thanks heaps! It felt like running in a circle
     
  7. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Hope you didn't wonder the internet for days like I did till you found this solution.
     
    gamesbynilo likes this.
  8. Samroski

    Samroski

    Joined:
    Sep 15, 2013
    Posts:
    1
    Think I may have the same issue. I have 2-4 audiosources attached to my prefab enemies, and I cannot get them to play as I would like. Some sounds seem to take over, while others do not play, and the destruction explosions do not play at all, which will likely resolve with your solution of dismissing them off screen before destroying. Still worried about the other strange behavior, but thanks for the explanation.
     
  9. sompod44

    sompod44

    Joined:
    Nov 2, 2018
    Posts:
    3
    Hello Coders!!!
    This problem occur when we try to play a sound from a prefabs and that moment we wanna destroy the gameobject.
    So all things are happen in a min time so that we thing maybe sound clips are not playing.
    so we need some time for playing the audio clip for playing then need gameobject destroy.
    So the simply code is :
    Sound.play();
    while (!Sound.isPlaying)
    {
    Destroy(gameObject);
    }
     
  10. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    I encountered this problem.Any advice?
     
    audinhugo likes this.
  11. Krautbytes

    Krautbytes

    Joined:
    Aug 11, 2017
    Posts:
    1
    For all still encountering this problem:

    For me somehow the "Mute Audio" was enabled at the Game Window. This fixed it for me.

    upload_2021-2-19_12-29-43.png
     
  12. leckerekochbanane

    leckerekochbanane

    Joined:
    Jun 5, 2020
    Posts:
    1
    Krautbytes thank u so much man
     
  13. miguelebs38

    miguelebs38

    Joined:
    Dec 11, 2019
    Posts:
    1
    I dont know if anyone has solved this, but you could move the item offscreen and use a coroutine to destroy it after X seconds
     
  14. HeyBishop

    HeyBishop

    Joined:
    Jun 22, 2017
    Posts:
    238
    Weird. You'd think "Play On Awake" checkbox would be enough, eh?
     
  15. michaelchambers

    michaelchambers

    Joined:
    May 12, 2020
    Posts:
    1
    Code (CSharp):
    1. [RequireComponent(typeof(AudioSource))]
    2.  
    3. public class myGameObject : MonoBehaviour
    4. {
    5.     private void OnTriggerEnter(Collider other)
    6.     {
    7.         gameObject.GetComponent<AudioSource>().Play();
    8.  
    9.         // Hide Mesh Texture
    10.         gameObject.GetComponent<MeshRenderer>().enabled = false;
    11.  
    12.         // Destroy object at end of audio play
    13.         Destroy(gameObject, gameObject.GetComponent<AudioSource>().clip.length);
    14.     }
    15. }