IEnumerator not working.WaitForSeconds never continiues.
When my character dies a canvas pops up and i want the sound to stop after 2 seconds.I've tried IEnumerator with coroutine but it seems that it never executes.The Debug.Log ("Start") appears in console,but the End never executes.Any ideas? Here is the code.
public void makedead ()
{
Destroy(gameObject);
Instantiate (DeathFXPrefab, transform.position, transform.rotation);
AudioSource.PlayClipAtPoint (DeathSound, transform.position);
health = 0;
tryAgain.enabled = true;
Time.timeScale = 0.3f;
StartCoroutine (soundpause (2.0f));
}
IEnumerator soundpause(float sec)
{
Debug.Log ("Start");
yield return new WaitForSeconds (sec);
AudioListener.pause = true;
Debug.Log ("End");
}
Answer by WillNode · Jul 24, 2016 at 02:07 AM
I'm sure 99% of your problem comes from...
Destroy(gameObject);
You are destroying the game object, while it runs a coroutine. That's just make the coroutine stopped at the end of the frame when starting a coroutine.
One of the solution is put that destory function after the coroutine finished.
{
Instantiate (DeathFXPrefab, transform.position, transform.rotation);
AudioSource.PlayClipAtPoint (DeathSound, transform.position);
health = 0;
tryAgain.enabled = true;
Time.timeScale = 0.3f;
StartCoroutine (soundpause (2.0f));
}
IEnumerator soundpause(float sec)
{
Debug.Log ("Start");
yield return new WaitForSeconds (sec);
AudioListener.pause = true;
Destroy(gameObject);
Debug.Log ("End");
}
FYI, Time.timeScale
affects WaitForSeconds
timing. it will not really 2 seconds, but 2 / 0.3 seconds, which is much longer time.
Thanks for the answer,a part of the problem was this.I did exactly as you said and i deactivated time.timescale for the experiment.The coroutine completes it's circle,but it multiplies the DeathFx,and DeathSound so many times that makes the game nearly crash (my pc too).Sorry,but i'm new to all this and i have not fully understood the coroutine mechanisms.
Answer by SteliosDaVitzi · Jul 24, 2016 at 01:27 PM
Thanks to your answer,i finally did it!I placed the Coroutine in a different void which i called in OnTriggerEnter,and because the character had time to die again,i deactivated it's mesh renderer,speed,and collider.here is the script:
void Awake()
{
rb = GetComponent<Rigidbody> ();
tryAgain.enabled = false;
rend = GetComponent<Renderer> ();
rend.enabled = true;
isDead = false;
coll.enabled = true;
}
void FixedUpdate ()
{
transform.Translate (Input.acceleration.x * speedx * Time.deltaTime, 0, +Input.acceleration.y * speedy * Time.deltaTime);
rb.AddForce(0,0,0);
}
public void makedead ()
{
health = 0;
Instantiate (DeathFXPrefab, transform.position, transform.rotation);
AudioSource.PlayClipAtPoint (DeathSound, transform.position);
rend.enabled = false;
isDead = true;
speedx = 0;
speedy = 0;
coll.enabled = false;
}
void Coroutine()
{
StartCoroutine (death (2.0f));
}
IEnumerator death(float sec)
{
Debug.Log ("Start");
yield return new WaitForSeconds (sec);
Destroy(gameObject);
tryAgain.enabled = true;
AudioListener.pause = true;
Time.timeScale = 0.1f;
Debug.Log ("End");
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy") {
Coroutine ();
makedead ();
}