Same GameObject Two AudioSources Not Playing Their AudioClips - Unity

74 Views Asked by At

I was working on 3d runner kind of game so I have attached two AudioSources component within Player GameObject:

  1. AudioSource for playing gameplay music
  2. 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.

enter image description here

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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.