- Home /
Stop audio playing after a timer reset
Hi I am very new to all this and have tried to solve most of my problems myself but I'm stuck on how to add in a 2nd condition to this Jscript.
I use 3 scripts.
I have an add time script which adds +10 seconds of time on an item pickup to two other scripts one counts down to 0 and restarts the level and one plays a audio file at 0 seconds but the countdown is 7 so the audio plays with 3 seconds of the level time left.
I want to add a condition into the audio script so the sound is stopped if the timer goes back above 0 (3 seconds level time)
I originally had 1 timer script but again got stuck on how the 'if' conditions worked. This solved one problem but created another.
Thanks in advance.
#pragma strict
var countdown : AudioClip;
var timer2 : float = 7.0;
function Update()
{
timer2 -= Time.deltaTime;
if(timer2 <= 0)
{
timer2 = 0;
AudioSource.PlayClipAtPoint(countdown, transform.position);
Destroy(gameObject);
}
}
Answer by markmmiller · Feb 02, 2014 at 04:05 PM
Switch your if statements around like this. That way your application will stop the audio until the timer is up. If the timer jumps back up above zero by any amount (1, 2 or 10 seconds etc) it should automatically stop the audio. I doubt it will be a graceful stop but you can investigate this yourself later.
Loop is below
if(timer2 > 0.0f)
{
AudioSource.Stop();
}
else if(timer <= 0.0f)
{
AudioSource.PlayClip etc..
}
Thanks that helps, the Loop at least works after changing some elements. However if I use Destroy(gameObject) the object is obviously removed and the sound doesn't play. If I don't use Destroy(gameObject)the audio layers up and plays over itself in the loop. Is there anyway I can make the audio not play if it is currently playing ? I've had a look at "if (!audio.isPlaying)" but can't get it to work.
Here is the current code:
function Update()
{
timer2 -= Time.deltaTime;
if(timer2 > 0.0f)
{audio.Stop();
}
else if(timer2 <= 0.0f)
{audio.PlayOneShot(countdown, 0.7);
Destroy(gameObject);
}
}
I'm not sure I understand what it is you are trying to do. What object are you needing to destroy?
Answer by sido · Feb 02, 2014 at 09:36 PM
Managed to solve this myself.
if(timer2 > 0.0f)
{
audio.Stop();
}
else if(timer2 <= 0.0f)
{
if (!audio.isPlaying){
audio.clip = countdown;
audio.Play();
Your answer
Follow this Question
Related Questions
Timer Scripting No Idea On Script 0 Answers
Need help with delaying a timer (javascript) 2 Answers
Countdown 2 Answers
Play Audio at the end of a Countdown 0 Answers