Prefabs not playing audio- SOLVED!

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?

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.

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.
public AudioSource audio;
  1. In your prefab GameObject, add an AudioSource component, by clicking the add component.
  2. Now drag what ever audio you want to play from your Assets on to the Audio Clip section of your newly created AudioSource component.
  3. 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)
  4. 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.

sound.Play();
transform.position = Vector3.one * 9999f; // move the game object off screen while it finishes it's sound, then destroy it
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.

3174334--241908--fix.jpg

11 Likes

I have the same problem for days. Now I finally solved it thanks to you. Great work!!!

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.

Thanks heaps! It felt like running in a circle

Hope you didn’t wonder the internet for days like I did till you found this solution.

1 Like

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.

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);
}

I encountered this problem.Any advice?

1 Like

For all still encountering this problem:

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

Krautbytes thank u so much man

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

Weird. You’d think “Play On Awake” checkbox would be enough, eh?

[RequireComponent(typeof(AudioSource))]

public class myGameObject : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        gameObject.GetComponent<AudioSource>().Play();

        // Hide Mesh Texture
        gameObject.GetComponent<MeshRenderer>().enabled = false;

        // Destroy object at end of audio play
        Destroy(gameObject, gameObject.GetComponent<AudioSource>().clip.length);
    }
}