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

Unity C# yeld WaitForSeconds don't work

Discussion in 'Scripting' started by Arkamek, Dec 9, 2014.

  1. Arkamek

    Arkamek

    Joined:
    Jul 4, 2014
    Posts:
    23
    Hello, i don't know why my script don't work, i think that i should see number from time every 2 second, but debug log msg only 1 time, and nothing more. Here is my script:


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Atak1 : MonoBehaviour {
    6.  
    7.     private bool ready = true;
    8.     private int time = 0;
    9.  
    10.     void Start () {
    11.     }
    12.  
    13.     void Update () {
    14.     if(ready){
    15.         StartCoroutine(test ());
    16.         }
    17.     }
    18.  
    19.     IEnumerator test()
    20.     {
    21.         ready = false;
    22.         Debug.Log ("Liczba: " + time);
    23.         time++;
    24.         yield return new WaitForSeconds(2);
    25.         ready = true;
    26.     }
    27.     }
    28.  
    As i said i see only:
    Code (csharp):
    1.  
    2. Liczba:0
    3. UnityEngine.Debug:Log(Object)
    in debug log 1 time and nothing after this.
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It works correctly for me. If you use only this script in an empty scene it will work for you as well.
    Are you deactivating the component or the game object in any way?
     
  3. Arkamek

    Arkamek

    Joined:
    Jul 4, 2014
    Posts:
    23
    No, i create new empty GameObject, add this script as new Component, click play and nothing. That what i said in 1st post :(
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you try it in an empty scene?
     
  5. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    The output you got is the correct output for the code you wrote. What are you actually trying to do?

    EDIT: I misread what your output was, apologies.
     
  6. Arkamek

    Arkamek

    Joined:
    Jul 4, 2014
    Posts:
    23
    It's not correct, after wait 2 second it should change bool ready to true, and after this Update void should get it and start Coroutine again.
     
  7. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Right click the inspector window with the gameobject with this script attached and change to "debug". Then you can see if the boolean is changing or not correctly.
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It is working correctly. I tested it. There is probably something else in the scene that deactivates the game object or component.
     
  9. Arkamek

    Arkamek

    Joined:
    Jul 4, 2014
    Posts:
    23
    It's changing only time INT 1x, from 0 to 1
    Code (csharp):
    1. time++;
    But code after
    Code (csharp):
    1. yield return new WaitForSeconds(2);
    is like invisible, because in debug mode it show that ready boolean, which should change to
    Code (csharp):
    1. ready = true;
    after 2 seconds are still like before: False
     
  10. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Also, are you actually waiting the 2 seconds for the time to increment again?

    It's actually correct that it will still show false as it almost instantly set to false when it becomes true anyway
     
  11. Arkamek

    Arkamek

    Joined:
    Jul 4, 2014
    Posts:
    23
    Do you have any idea where should i search this error?

    #UP Yes, but it's not change to true after 2 seconds
     
  12. Arkamek

    Arkamek

    Joined:
    Jul 4, 2014
    Posts:
    23
    Ok i fixed that, problem was cuz i has changed Timescale to 0... Thanks all for help
     
  13. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Again, did you try it in an empty scene where you just have a game object with this script?
    The answer is no, because I have tested it and it works.

    Did you check whether the game object or the component is somehow deactivated?
    No, you didn't.

    The cause why the execution doesn't continue is usually that the game object or the component is deactivated. It would kind of make sense to check that.

    Edit: Or the time scale is set to zero ;)
     
    Arkamek likes this.
  14. RockoDyne

    RockoDyne

    Joined:
    Apr 10, 2014
    Posts:
    2,234
    I think you're confused about how coroutines work. The point of the yield statement is that the function stops executing there, and the next time it's called, it continues running right where you left it. If you don't yield in the middle of a loop, they don't repeat. Unless you have something like:
    Code (CSharp):
    1. while(supposedToRun){
    2.        Debug.Log ("Liczba: " + time);
    3.         time++;
    4.         yield return new WaitForSeconds(2);
    5. }
    it won't repeat.
     
  15. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The original code works perfectly fine. It will be started over and over again because that happens in the Update method.