I use this code:
using UnityEngine;
using System.Collections;
/// <summary>
/// Creating instance of sounds from code with no effort
/// </summary>
public class SoundEffectsHelper : MonoBehaviour
{
/// <summary>
/// Singleton
/// </summary>
public static SoundEffectsHelper Instance;
public AudioClip explosionSound;
public AudioClip playerShotSound;
public AudioClip enemyShotSound;
void Awake()
{
// Register the singleton
if (Instance != null)
{
Debug.LogError("Multiple instances of SoundEffectsHelper!");
}
Instance = this;
}
public void MakeExplosionSound()
{
MakeSound(explosionSound);
}
public void MakePlayerShotSound()
{
MakeSound(playerShotSound);
}
public void MakeEnemyShotSound()
{
MakeSound(enemyShotSound);
}
/// <summary>
/// Play a given sound
/// </summary>
/// <param name="originalClip"></param>
private void MakeSound(AudioClip originalClip)
{
// As it is not 3D audio clip, position doesn't matter.
AudioSource.PlayClipAtPoint(originalClip, transform.position);
}
}
From Making some noises on music — Pixelnest Studio. It works okay. But I want to not allow the same type of AudioClip to be used more than once at the same time. I tried adding if (!audio.isPlaying) play_it
and rewriting PlayClipAtPoint
to Play
, but it didn’t work, because I guess audio is the same for all of these audioclips. What can I do?