I was working on 3d runner kind of game so I have attached two AudioSources component within Player GameObject:
- AudioSource for playing gameplay music
- AudioSource for playing player collision sound
On same Player GameObject, I have assigned two AudioSources for playing these two sounds but at present only GamePlay music is playing, Player collision sound not gets played when it collide with obstacles.
Both AudioSources have their assigned AudioClips.
On collision time with an obstacle, I was playing collision sound like this way:
void OnCollisionEnter (Collision other)
{
if (other.transform.CompareTag (GameConstants.TAG_OBSTACLE)) {
Vector3 splashEffectPos = groundCheck.position;
if (splashEffectPos.y < 0.05f)
splashEffectPos.y = 0.05f;
// stop game music
if (SoundManager.Instance.EnableSound) {
gameSoundAS.Play ();
gameMusicAS.Stop ();
// gameSoundAS.PlayOneShot (ballCollisionClip);
}
GameObject splash = Instantiate (splashEffectPrefab, splashEffectPos, Quaternion.identity);
splash.transform.SetParent (GameController.Instance.transform);
GameController.Instance.GameOver ();
gameObject.SetActive (false);
}
}
GamePlay music is playing properly and get stopped when it collide with an obstacle but Player collision sound is not playing.
Okay, I managed to solve the problem myself. If GameObject becomes disabled then its AudioSources isn't able to play.
In the
OnCollisionEnter
method above, I have disabled my Player GameObject so AudioSource won't produce any sound.