Playing multiple audio clips
I am trying to play 2 samples in 1 script, 1 for when enemy gets hit and 1 for when it dies. But for the life of me I cannot get more than 1 sample to play. I have tried about 5 or 6 methods from googling the issue and none of them work. I really don't see how it is so difficult to play more than 1 sound effect. Have tried the AudioSource[] AudioClips = null; and using AudioClips[0].Play(); but that still didn't play the second audio clip. The method I am using now doesn't work. Can someone give me a way to play 2 samples that works? Will add all the code in my script just for clarity and an image of how the inspector is set up.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class Enemies : MonoBehaviour {
public float movespeed = 5;
public int starthealth = 50;
private int curhealth;
public Transform explode1;
private float x;
private float y;
private float z;
public AudioClip Pig1;
public AudioClip Pig2;
public AudioSource source1;
public AudioSource source2;
void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag == "Prefab")
{
Destroy(col.gameObject);
curhealth = curhealth - 10;
if(curhealth > 9)
source1.PlayOneShot(Pig1,1f);
}
if(curhealth <= 0)
{
source2.PlayOneShot(Pig2,1f);
x = gameObject.transform.position.x;
y = gameObject.transform.position.y;
z = gameObject.transform.position.z;
Instantiate(explode1, new Vector3(x, y, z), Quaternion.identity);
Destroy(gameObject);
}
}
void Awake ()
{
source1 = GetComponent<AudioSource>();
source2 = GetComponent<AudioSource>();
curhealth = starthealth;
}
void Update () {
transform.Translate(Vector3.forward * movespeed * Time.deltaTime);
}
}
The FirstPersonController script from the asset has multiple samples playing so I copied how they used play audio clip in their script and matched inspector settings. AND IT STILL DOESN'T WOR$$anonymous$$!!! How the bloody hell do you play more than 1 sample? It's getting ridiculous now.
Answer by Positive7 · Jun 01, 2018 at 02:34 AM
source1 = GetComponents<AudioSource>()[0];
source2 = GetComponents<AudioSource>()[1];
Cannot apply indexing with [] to an expression of type `UnityEngine.AudioSource'
Thanks I found that out (plurals) after 30 $$anonymous$$utes of googling lol. Unfortunately using that method still only 1 of the samples would play.
Answer by Ramus1973 · Jun 02, 2018 at 04:14 AM
I have kind of found a solution but it is far from ideal. For some reason trying to use an audio source from the gameObject means only 1 sample will play. I created an empty object and attached an audio source to that and gave it the tag Audio. Then used _audio = GameObject.FindGameObjectWithTag("Audio"); and I can use 2 samples??? I just don't understand why I can't play 2 samples using the audio source from the gameobject itself? I'm assuming _audio = gameObject would get the audio source from the object the script is attached to? But doing that only 1 sample plays....... Completely confused by something as simple as playing 2 samples.
Your answer
Follow this Question
Related Questions
Why won't my second Audio Source play? 0 Answers
oneshot not working 0 Answers
Stop Audio Source Clip while button Press 1 Answer
can i make a loop for each with all audiosources of my project ? 0 Answers
Second AudioClip won't play 0 Answers