- Home /
Can't get sound clip from array to play properly
I have an array for 2 audiosources and I'm trying to make the first clip play when it hits the enemy and the second for when my character explodes. The first one works fine but when my character explodes the second one doesn't make a sound. Is there something wrong with my code?
AudioSource clip1;
AudioSource clip2;
void Start ()
{
AudioSource[] sounds = GetComponents<AudioSource>();
clip1 = sounds[0];
clip2 = sounds[1];
}
void FixedUpdate ()
{
GroundCheck ();
if (grounded == false || dragged == true)
{
float h = Input.GetAxis ("Mouse X");
if (h > 0 && !facingRight)
Flip ();
else if (h < 0 && facingRight)
Flip ();
}
if (grounded == true)
{
if (walking == true)
{
walker ();
}
// Create an array of all the colliders in front of the enemy.
Collider2D[] frontHits = Physics2D.OverlapPointAll (frontCheck.position);
// Check each of the colliders.
foreach (Collider2D c in frontHits) {
// If any of the colliders is an Obstacle...
if (c.tag == "DirectionTrigger") {
// ... Flip the enemy and stop checking the other colliders.
Flip ();
break;
}
if (c.tag == "Enemy") {
if (enemyhit == false){
if (c.gameObject.GetComponent<Bugbot> ().destructionImminent == false)
{
// ... Flip the enemy and stop checking the other colliders.
animator.SetTrigger("swat");
walking = false;
clip1.Play();
}
}
break;
}
}
}
if(HP <= 0 && !dead)
{
// ... call the death function.
Death ();
StartCoroutine(delayAnim());
}
}
public IEnumerator delayAnim()
{
yield return new WaitForSeconds(0.01f);
animator.SetTrigger ("swatbotexplosion");
clip2.Play();
}
Do you have a clip in the AudioSource?
First off do you get inside the coroutine? Place a print or a debug to see if the method is reached.
yeap, I just checked now and it goes in. I also have a clip in the audio source but from what I notice is that it's always the first one called that plays.
the second one only plays when i exit the play scene or when the game over scene pops up.
Answer by gimoj · Jul 19, 2014 at 10:55 AM
Nevermind the clips themselves were the problem. I got it to work after changing them. Thanks and sorry for the trouble.
Your answer
Follow this Question
Related Questions
Read audio/music currently playing on speakers? 0 Answers
How to start a sound not from it's start ? 2 Answers
[Rhytm game] How to spawn on exact time of music? 3 Answers
Audio is always playing 0 Answers
How to avoid audio glitches whilst switching volume of an Audiosource each Update()-Frame? 0 Answers