Hello everybody!
I guess this is a common issue but I can´t find a tutorial to help me solve this problem.
The thing is that, in my game, at certain points, lots of enemies are destroyed simultaneously, and because of that a lot of destroy Sound FX play at the same time, which makes a strange noise, like the speakers are about to break x)
The way it works is that each enemy has a Prefab with the SFX attached, and it destroys itself once it has ended playing.
If you know how to solve this problem, please help me.
This is the code:
public class SFX_Instantiation : MonoBehaviour
{
//SFX
public AudioClip SFX_ToPlay;
//DELAY SFX
public float setDelayTime;
float delayTimer;
bool hasPlayed;
private AudioSource source;
int instanceNum;
private void Awake()
{
source = GetComponent<AudioSource>();
}
void Start()
{
delayTimer = 0;
hasPlayed = false;
}
void Update()
{
delayTimer += Time.unscaledDeltaTime;
if (delayTimer >= setDelayTime && !hasPlayed)
{
source.PlayOneShot(SFX_ToPlay, volumeScale: 1);
hasPlayed = true;
}
if (delayTimer >= setDelayTime + 0.1f &&
source.isPlaying == false)
{
Destroy(gameObject);
}
}
}