No audio clip plays onmousedown event

Hello to everyone! i make a memory card game. The function below shows that OnMouseDown event i flip the card and a sound of a flipped card is playing for once and then does a check for any similar flipped other cards in the game. That js code is attached to a card object in the game called card_1 (made manually in the Hierarchy). Also i attached an Audio Source on this object, and the audio clip option has the audio clip from my Resources (3d sound). I have a music plays on the background of the main game (main camera), that’s for the record.

public var hasPlayed:boolean=false;
public var flipSound : AudioClip;
public var card_1: GameObject;
card_1 = GameObject.Find("card_1");
     
    function OnMouseDown () {
    card_1.transform.localRotation.z = 180;
    Invoke("CheckCards", 1);
    	if(hasPlayed == false){
    	    audio.PlayOneShot(flipSound);
    	    hasPlayed = true;
    	}
    	hasPlayed = false;
    }

Unfortunately the audio.PlayOneShot(flipSound); seems it doesn’t work at all. What’s the problem?
Thanx in advance!

Since you are using 3D sounds, my first guess would be that the game object with the audio source component is too far away from the audio listener. Check to make sure the audio listener is within range of the audio source that is responsible for playing the "flip sound". If it isn't, you can either increase the audio sources "Max Distance" property, or import the audio clip as 2D, that way the audio listener will pick up the sound regardless of distance from the card object.

@Key_Less thanx for ur reply. I increased the audio source's Max Distance from 500 to 1000 or 2000 for example but nothing did happen :( Another thing which i didn't post above is that on the inspector i haven't set to my variable flipSound the actual sound clip (from Resources), i only added that sound clip to the audio source property called "Audio clip" and only. I don't know if does affect anything... Now, as for your 2nd option, i don't know how to import the sound clip as a 2D sound. Thanx again for ur time! :))

Well, i found it why. As a solution comes that i unticked the audio source's property called "Play on awake". And so it worked! Thanx @Key_Less!

1 Answer

1

Hey Makeiteasy,

as Key_less already stated, you could try to import the music as 2D, to do this is quite simple. Simply select the desired sound in the “Project” Tab, then in the “Inspector” tab untick “3D Sound”. Hit apply and that’s it!

Hopefully this will fix your problem!

Edit: Nvm Just found out you managed to fixed your problem!

Ful beat me to it. Your sound asset in the project window, when selected, has options in the Inspector. One of them is to toggle off the 3d sound, which means the distance between the audio source and the audio listener will not affect the playback volume. The follow up question seems related to your audio source (in the hierarchy window) having an option to play on awake.