- Home /
Stop Specific Audio on a Script with multiple Sounds?
Hi, I have 2 sounds on a script a fire sound and a thruster sound. How to I stop 1 specifically on 1 script because right now if I move forward, my thruster sound goes and loops and is fine, but when I shoot while moving forward then my fire sound goes and loops and the thruster stops. I just want to have the thruster loop while I hit W and the fire not loop but play once when I shoot, they are both in the same script and same game object. Here is my code.
using UnityEngine;
using System.Collections;
public class Shooting_CJ : MonoBehaviour {
public AudioClip Laser;
public AudioClip Thruster;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("space"))
{
audio.clip = Laser;
audio.Play();
}
if (Input.GetKeyDown(KeyCode.W))
{
audio.loop = true;
audio.clip = Thruster;
audio.Play();
}
if (Input.GetKeyUp(KeyCode.W))
{
audio.Pause();
audio.loop = false;
}
else
{
}
}
}
Answer by idunlop_oefun · Oct 23, 2012 at 06:36 AM
Refactor your script / game object arrangement and use two AudioSource's - one for the thruster and one for the laser.
Answer by spiceboy9994 · Oct 24, 2012 at 12:29 AM
Have you tried to use audio.PlayOneShot() for your clip that you want to play just once?. For the other one you could use the audio.Play()
Let me know if that works for you.
Your answer
Follow this Question
Related Questions
Play Looped Sound? 4 Answers
Sound not looping properly on device? 2 Answers
How to synchronize sound and animation loop? 2 Answers
Always play music? 1 Answer
How do I fix audio loop delay 2 Answers