PlayClipAtPoint() Volume doesn't use the object's Audio Source

PlayClipAtPoint() doesn’t use a game object’s Audio Source component so you won’t be able to control the volume of your sfx from the object’s Audio Source IF you are using PlayClipAtPoint(). You can still control the volume from the inspector by serializing the volume argument of the PlayClipAtPoint() method like this.

public class CoinPickup : MonoBehaviour
{
    [SerializeField] AudioClip coinPickupAudio;
    [Range(0f,1f)] [SerializeField] float volume = 1f;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Player")
        {
            AudioSource.PlayClipAtPoint(coinPickupAudio, Camera.main.transform.position, volume);
            Destroy(gameObject);
        }
    }
}

You can then adjust the volume from CoinPickup (Script) component in the inspector.

3 Likes

Extending on this, I don’t think you need to add an AudioSource Component to your prefab at all. The Unity Docs say that calling AudioSource.PlayClipAtPoint() creates a temporary AudioSource, destroyed once the clip has played.

Everything runs just fine and as expected when no AudioSource Component is present.

2 Likes

Privacy & Terms