I making a Enemyspawning code that will spawn enemy at the exact time wit coroutine which work well for me
but when my character die I want the coroutine to start from the beginning again.
Any Idea I can do that?
Thnx for the help >W<
I making a Enemyspawning code that will spawn enemy at the exact time wit coroutine which work well for me
but when my character die I want the coroutine to start from the beginning again.
Any Idea I can do that?
Thnx for the help >W<
As Sergio7888 said, you would need to manually stop the coroutine then start it again. You can write some Extension Methods that can handle that that for you.
using UnityEngine;
using System.Collections;
public static class CoroutineExtensions
{
/// <summary>
/// Tries to stop a coroutine based on a Coroutine Handle.
/// will only stop the Coroutine if the handle is not null
/// </summary>
/// <returns>the Monobehaviour script running the coroutine, allowing chained commands</returns>
/// <param name="handle">Handle.</param>
public static MonoBehaviour TryStopCoroutine(this MonoBehaviour script, ref Coroutine handle)
{
if (handle != null)
script.StopCoroutine (handle);
return script;
}
/// <summary>
/// Starts the coroutine and sets the routine to a Coroutine handle.
/// </summary>
/// <returns>the Monobehaviour script running the coroutine, allowing chained commands</returns>
/// <param name="routine">Routine.</param>
/// <param name="handle">Handle.</param>
public static MonoBehaviour StartCoroutine(this MonoBehaviour script,IEnumerator routine, ref Coroutine handle)
{
if(!script.enabled || !script.gameObject.activeInHierarchy)
{
Debug.LogErrorFormat (script, "The Script {0} is currently disabled and cannot start coroutines", script);
return script;
}
handle = script.StartCoroutine (routine);
return script;
}
/// <summary>
/// Stops any possible coroutine running on the specified handle and runs a new routine in its place
/// </summary>
/// <returns>the Monobehaviour script running the coroutine, allowing chained commands</returns>
/// <param name="script">Script.</param>
/// <param name="routine">Routine.</param>
/// <param name="handle">Handle.</param>
public static MonoBehaviour RestartCoroutine(this MonoBehaviour script, IEnumerator routine, ref Coroutine handle)
{
return script.TryStopCoroutine (ref handle)
.StartCoroutine (routine, ref handle);
}
}
With this, any Monobehaviour script can start, restart, and safely stop (if its running) any coroutine its running by handle.
public class BombScript : MonoBehaviour
{
public Coroutine handle;
void OnCollisionEnter(Collider other)
{
//when a player touches the bomb it will start to countdown, touching the bomb again resets the countdown
if(other.GetComponent<Player>() != null)
//being an extension method, you'll need to use the "this" keyword to access it
this.RestartCoroutine (CountDown(5f),ref handle);
}
private IEnumerator CountDown(float timer)
{
YieldInstruction wait = new WaitForSeconds (1);
Debug.Log ("Bomb will explode in...");
while (timer > 0)
{
Debug.Log (Mathf.CeilToInt(timer));
timer -= 1;
yield return wait;
}
Explode ();
}
private void Explode()
{
Debug.Log ("You Lose!");
}
}
Thnx a lot!!!Now it’s working >W<