- Home /
Stop AudioClips by script (c#)
How can i do to stop the audioclips that i've declared in a script? for example:
public AudioClip Audio1;
public AudioClip Audio2;
public AudioClip Audio3;
void update()..
if (Input.GetKeyDown (KeyCode.N)) {
audio1.volume = 0.0;
audio2.volume = 0.0,
audio3.volume = 0.0;
}
Thank you in advance
Answer by chufraise · Apr 19, 2014 at 04:03 PM
Use AudioSource
instead, and assign your AudioClip
to AudioSource.clip
.
Good luck!
ok i make so, but unity editor gives me this error: Literal of type double cannot be implicitly converted to type float'. Add suffix
f' to create a literal of this type
public class Scream : $$anonymous$$onoBehaviour {
public AudioSource Audio1;
public AudioSource Audio2;
public AudioSource Audio3;
public AudioSource Audio4;
public AudioSource Audio5;
public AudioSource Audio6;
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.N)) {
Audio1.volume = 0.0;
Audio2.volume = 0.0;
Audio3.volume = 0.0;
Audio4.volume = 0.0;
Audio5.volume = 0.0;
Audio6.volume = 0.0;
}
.
.
.
.
.
Yes, you should add a float suffix. Like:
Audio1.volume = 0.0f;
ins$$anonymous$$d of
Audio1.volume = 0.0;
Ah ok, but there are other problems with the other parts of the script, now i show you the full script, the message error now is: The best overloaded method match for `UnityEngine.AudioSource.PlayOneShot(UnityEngine.AudioClip)' has some invalid arguments what does it mean?
public AudioSource Audio1;
public AudioSource Audio2;
public AudioSource Audio3;
public AudioSource Audio4;
public AudioSource Audio5;
public AudioSource Audio6;
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.N)) {
Audio1.volume = 0.0f;
Audio2.volume = 0.0f;
Audio3.volume = 0.0f;
Audio4.volume = 0.0f;
Audio5.volume = 0.0f;
Audio6.volume = 0.0f;
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.A)) {
StartCoroutine (Wait$$anonymous$$ethod ());
}
else if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.D)) {
StartCoroutine (Wait$$anonymous$$ethod2 ());
}
}
IEnumerator Wait$$anonymous$$ethod(){
yield return new WaitForSeconds(2f);
audio.PlayOneShot(Audio1);
audio.PlayOneShot(Audio2);
audio.PlayOneShot(Audio3);
audio.Play();
}
IEnumerator Wait$$anonymous$$ethod2(){
yield return new WaitForSeconds(2f);
audio.PlayOneShot(Audio4);
audio.PlayOneShot(Audio5);
audio.PlayOneShot(Audio6);
audio.Play();
}
}
Ah, ok. You mixed things up a bit. Either you declare AudioClip
variables as you initially did. Then to change the volume you just have to do it on the AudioSource
component, like: audio.volume = 1f;
Then to play them you type: audio.PlayOneShot(clip);
The other way is to declare AudioSource
variables, and set their volumes individually, like Audio1.volume = 1f;
Then to play them you type: Audio1.Play();
Hope it helps!
mhmh i don't understand very well, so i have to declare the audioclip at the start as i already did, and then in void update set the "audio.volume = 0f;"?