I am having problems with PLayClipAtPoint()
. I want to play a given sound as soon as a gameObject is destroyed. Here is my code (the concerned parts):
void Update()
{
if (health <= 0)
{
StartCoroutine(Death());
}
}
public IEnumerator Death() {
anim.SetBool("isDying", true);
AudioSource.PlayClipAtPoint(dmgSounds[0], transform.position);
Destroy(gameObject, 3f);
yield return null;
}
I assume that the problem is due to the fact that as soon as the conditions are achieved, in this case the health reaching zero, the coroutine starts and it will keep starting for each frame, but how else should I call it? Anybody has some hint to give?
EDIT: I have already tried to wrap the logic in a bool as suggested in the comment so that it would work only when the bool condition is valid, without success. Here is the code with the flag bool:
void Update()
{
if (health <= 0)
{
if(isAlive){
isAlive = false;
StartCoroutine(Death());
}
}
}
bool
flag to your object and setting it totrue
when the object dies and you start the coroutine, and don't call the coroutine again if thebool == true
;? \$\endgroup\$ – Vaillancourt♦ Aug 22 '19 at 19:56