- Home /
 
Unity plays only a few gameobject's sound
I'm developing a 2D game, and have a Button prefab what plays a sound when clicked.
I have multiple instance of this prefab on my scene, but only 2 of them plays the sound, the others don't play anything, or just a very short something.
I thought that it is caused by the distance of the camera and the gameObject, but if I place a button what is silent, to the position of a working button, it still won't play anything.
My code is simple:
 ...
 public AudioSource myAudioSrc;
 
 public AudioClip btnSound;
 ...
 protected virtual void OnMouseUpAsButton() {
     if(!isEnabled) { return; }
     myRenderer.sprite       = normalImg;
     transform.localScale    = normalSize;
 
     myAudioSrc.PlayOneShot(btnSound);
     Debug.Log(gameObject.name);
 
     //üzenet küldése az objektumnak a message metódus hívására az data paraméterrel
     target.SendMessage(message, data);
 }
 ...
 
               EDIT:
I've got this working with the code:
 AudioSource.PlayClipAtPoint(btnSnd, Camera.main.transform.position);
 
               So it has to do something with the distances.
Is there any better solution?
Answer by klolololol · Jul 21, 2015 at 11:14 AM
It turns out I disabled the gameobject with the OnMousUpAsButton event... It's embarassing
Answer by BiG · Jul 21, 2015 at 10:23 AM
If you are developing a 2D game, and you think that the problem relies on distances, you can just use 2D sound (if you are using Unity5, open the AudioSource and set Spatial Blend = 2D; or click on the sound asset and uncheck the 3D Sound checkbox).
Your answer