Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trouble with making a timer

Discussion in 'Scripting' started by wilsonc911, Aug 28, 2014.

  1. wilsonc911

    wilsonc911

    Joined:
    Aug 8, 2014
    Posts:
    9
    Code (CSharp):
    1.     IEnumerator KillZombie() {
    2.         yield return new WaitForSeconds(zombieDeathDelay); // waits for zombie corpse removal delay
    3.         PhotonNetwork.Destroy(gameObject);
    4.         Debug.Log("Zombie Died");
    5.         nm.zombieDied();
    6.     }
    Trying to run this code to do a timer. However, the code never acutally runs.

    ZombieDeathDelay = 5f;
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    You need to start the coroutine. There are 2 ways.
    StartCoroutine(KillZombie());
    and
    StartCoroutine("KillZombie");

    The latter allows you to call StopCoroutine("KillZombie"); as the other one does not.
     
    wilsonc911 likes this.
  3. wilsonc911

    wilsonc911

    Joined:
    Aug 8, 2014
    Posts:
    9
    How would I make it stop as soon as the zombie dies?
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    function OnDestory() {
    StopCoroutine("KillZombie");
    }