AudioSource refuses to Play

I have an audio source that’s assigned in my script. In the update function, I have the following code:

if (CoverTimer < 0.5f && SndCoverMove != null && !SndCoverMove.isPlaying)
{
    SndCoverMove.Play();
    Debug.Log("Play cover sound");
}

When I run the game, the sound does not play, and the log shows about thirty instances of “Play cover sound”, one frame after another. So, even though Play() is getting called, isPlaying never gets set to true.

I’ve tried setting the sound to “Play on Awake” and it plays fine then, but not when called from code, so the actual AudioSource is set up correctly, and so is the clip that’s assigned to it. I’ve tried setting the audio source’s priority to 1, with no effect.

This is driving me crazy… I’ve been using Unity for a long time now, but I’ve never seen the audio source silently fail like this before. Does anyone know of any arcane conditions that I may be missing to get this to work?

Edit: Found the problem. When the audio source was created, it was duplicated from another audio source. Another script, for whatever reason, decided to keep a reference to the duplicate rather than the original, so that completely unrelated script was preventing it from playing. Thanks for the help and sorry for wasting your time.

Audio Source Screenshot

From the top of my head, I believe you have to call audioSource.Play() only once, otherwise it keeps restarting the playback from the beginning. Try using Debug.Log (SndCoverMove.IsPlaying());

–

Yes, the scene window. Are you sure you are dragging them into the Scene window, not the Game window? Also, select all the PNG's and make sure that Texture Type is set to Sprite (2D and UI), not the Default. You can change the Texture Type in the Inspector when you select all the PNG's in the Project window.

–

2 Answers

2

I think there’s a host of Arcane sound bombs out there. Such as null references in some other script…forgot to drag a prefab into a slot. etc…could be wrong… Sometimes I try to set a timer instead of checking to see if it is playing, I can’t remmeber now but it seems like that check to see if Audio is playing, never worked… i’m not an expert, if it ever was buggy, maybe it’s fixed now, I am curious to see where this all goes.

No, I've been using Unity long enough to check all that. If it wasn't set in the inspector, the debug logs would never fire. If the audio clip or source had a problem, it wouldn't have worked when I set play on awake. Thanks for the suggestions, but it's got to be something else.

–

It works! I just needed to change the type to sprite to what you said!! Thanks a bunch!

–

Read through the other convo you’ve had…

That does seem weird and like a bug… but I’d think if it were a bug, a lot more people would be complaining about it. So, a couple guesses…

You sure your AudioSource reference is the right one? Double check… maybe even re-drag it in or if you don’t do it through property inspector, be very sure you’re finding it correctly.

You sure you don’t accidentally stop the source each frame too?

One other suggestion for troubleshooting purposes only… drop your current logic for playing the sound, and use a bool… set it to false initially. Play the sound when this is false, and then set it to true. Again, just to troubleshoot. You should hear your sound once this way.

I just double checked it is set to the correct audio source. I even re-applied it. And I added the following code in update, along with a class-scope bool: if(!PlayOpen) { SndCoverMove.Play(); Debug.Log("Force play"); PlayOpen = true; } Nothing plays, but the Debug.Log does go off once.

–