- Home /
Playing audio
I am tring to make this audio clip play ever time that the ship.active == true; but it is only happening the first time this happens. I know it is because of the audio.PlayOneShot but I cant find a way to use two audio sources and play then both ever time the ship.active == true;
Here is the code I got:
using UnityEngine;
using System.Collections;
public class DeathRemains : MonoBehaviour {
public GameObject shipfire1; //This is the fire that is on this ship when it is destroyed
public GameObject shipExplode1;//The explosion for the ship
public GameObject ship; //The ship
public AudioClip fireAudio;
public AudioClip explodeAudio;
void Start()
{
if( ship.active == true) // checks if what is in ship is active
{
shipExplode1.GetComponentInChildren<ParticleSystem>().Play();
PlayfireAudio();
PlayexplodeAudio ();
}
}
void PlayfireAudio ()
{
audio.PlayOneShot (fireAudio);
}
void PlayexplodeAudio ()
{
audio.PlayOneShot (explodeAudio);
}
void Update()
{
if( ship.active == true) //Checks if the ship is active
{
shipfire1.GetComponentInChildren<ParticleSystem>().Play(); //turns on the fire
}
}
}
Answer by phxvyper · Dec 25, 2012 at 12:20 AM
I suggest you use AudioSource instead of AudioClip, this is more manipulative than AudioClip. A simple example of using it in your case would be like so:
using UnityEngine;
using System.Collections;
public class DeathRemains : MonoBehaviour {
public AudioSource fireAudio;
public AudioSource explodeAudio;
void Start() {
}
void Update() {
if (ship.active) {
fireAudio.Play();
explodeAudio.Play();
}
}
}
~Make sure that you assign a clip to each audio source through the editor (and the class needs to be attached to an object, obviously).
AudioSource - http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
audio source not playing the audio clip 2 Answers
play sound on click 2 Answers
play audio for few seconds on key hit 1 Answer
Distribute terrain in zones 3 Answers