- Home /
Disabling a script inside a coroutine(look at my comment below)?
Hi I wrote a script to play a sound after the if statement activates, but nothing happens. I've tried a lot of different things. Here is the script I'm using:
using UnityEngine;
using System.Collections;
public class TimerScript : MonoBehaviour
{
public GUIText clockTime;
public GUIText loseMessage;
public GameObject rift;
public GameObject tryAgainButton;
public GameObject returnButton;
public Rigidbody kineticBody;
private float timeLeft;
public AudioClip[] loseSound;
void Start ()
{
kineticBody = GetComponent<Rigidbody> ();
timeLeft = 30;
clockTime.text = "Time: 00:" + timeLeft.ToString();
loseMessage.text = "";
}
void Update ()
{
timeLeft -= Time.smoothDeltaTime;
clockTime.text = "Time: 00:" + timeLeft.ToString ();
if (timeLeft <= 0)
{
loseMessage.text = "You Failed!";
clockTime.text = "Time: 00:00";
tryAgainButton.SetActive(true);
returnButton.SetActive(true);
kineticBody.isKinematic = true;
rift.SetActive(false);
PlaySound(0);
}
}
void PlaySound (int noise)
{
audio.clip = loseSound[noise];
audio.Play ();
}
}
I'm new to Unity so I'm not sure what I'm doing wrong.
Answer by supernat · Mar 24, 2015 at 06:56 AM
It's probably 1 of 2 things or both. 1) make sure you assigned a sound clip to the loseSound array in the Inspector. 2) Your Update() method is going to run every frame, so once the timer runs out, you're going to call PlaySound every frame which will restart your clip to the first sample every frame. You can fix this by wrapping it with a "if (!audio.isPlaying)" check before calling PlaySound, or you could add a new flag "gameOver" which you set to true at line 40 and test that it is false at line 31.
I've got another problem. After PlaySound(); or line 39, I want to disable the whole script after a few seconds. The reason I want to disable the script is ,because I no longer need it after PlaySound();.
P.S. I've changed the void PlaySound() into an IEnumerator.
P.S.S. Here is the updated script if you want to look at it:
using UnityEngine;
using System.Collections;
public class TimerScript : $$anonymous$$onoBehaviour
{
public GUIText clockTime;
public GUIText lose$$anonymous$$essage;
public GameObject tryAgainButton;
public GameObject returnButton;
private float timeLeft;
public AudioClip loseSound;
void Start ()
{
timeLeft = 30;
clockTime.text = string.Format("{0:0.0}", timeLeft);
lose$$anonymous$$essage.text = "";
}
void Update ()
{
timeLeft -= Time.deltaTime;
clockTime.text = string.Format("{0:0.0}", timeLeft);
if (timeLeft <= 0)
{
Time.timeScale = 0f;
lose$$anonymous$$essage.text = "You Failed!";
tryAgainButton.SetActive(true);
returnButton.SetActive(true);
if(!audio.isPlaying)
{
StartCoroutine(PlaySound());
}
}
}
IEnumerator PlaySound ()
{
audio.clip = loseSound;
audio.pitch = Random.Range (0.5f, 0.6f);
audio.Play ();
yield return new WaitForSeconds (audio.clip.length);
//I want to disable the script here
}
}
Are you saying you never need the script again after the sound has finished or just want the sound to stop playing?
If you just want the sound to stop then just remove looping in the inspector or you could add audio.Stop() immediately after the yield.
Also, I'm not sure if that will work as you are setting timeScale to 0 and then trying to perform actions after it's set to zero. I could be wrong there though, as I tend to use a gamePaused flag (bool) rather than zero time scale.
Which version of Unity are you using by the way? I would strongly recommend switching to 4.6 or 5 and use the new UI system if you haven't already.
Yeah I don't need the script after the sound. $$anonymous$$y audio isn't on loop, but it still plays strangely enough. Also, if timeScale = 0 isn't what I'm looking for, how do I implement the gamePaused flag. Finally, the version of Unity I'm using is 4.6, but I might upgrade to 5 in the future.
Yes, you need to put your timer in a while loop with a yield, so that the timer counts down before the rest of your code is activated.
while(timeLeft > 0){
timeLeft -= Time.deltaTime;
yield return null;
}
//Once the timer has finished, do everything else
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
using Aim Constraint 1 Answer
Passing Java String to C# for use in Wwise Post Event = Fail 0 Answers
Add audio. 1 Answer