Search Unity

ultra easy yield wiatforseconds and it does't work

Discussion in 'Scripting' started by shadow_monk, Dec 3, 2014.

?

i hvae an error at yield WaitForSeconds(2); whats going on

  1. you mono developmen is suropted

    1 vote(s)
    100.0%
  2. other reason

    0 vote(s)
    0.0%
  1. shadow_monk

    shadow_monk

    Joined:
    Dec 3, 2014
    Posts:
    7
    and whats going on here?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript1 : MonoBehaviour {
    5.     GameObject obiekt;
    6.     int xx ;
    7.      bool aaa=true;
    8.  
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     void Update () {
    14.         if (aaa) {waitt();}
    15.                 }
    16.    
    17. void waitt()
    18.     {
    19.         aaa=false;
    20.         yield WaitForSeconds(2);
    21.     obiekt.SetActive(true);
    22. }
    23. }
     
  2. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    Last edited: Dec 3, 2014
  3. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    all of those thinngs that GetupKid said.

    But in addition, if you want your coroutine to keep running every two seconds forever, you need to put an infinite loop in it, ironically.

    like this:

    Code (CSharp):
    1.     IEnumerator waitt()
    2.     {
    3.         while(true)
    4.         {
    5.             aaa=false;
    6.             yield WaitForSeconds(2);
    7.             obiekt.SetActive(true);
    8.         }
    9.     }
    If you don't do that, it will run once to completion, and then stop running. Although in the case you posted, it would do the first half, wait for two seconds, do the second half, and then stop running (maybe that's what you want? )
     
  4. shadow_monk

    shadow_monk

    Joined:
    Dec 3, 2014
    Posts:
    7

    the code was supposed to turn on a gameobject after 2s. thx for your help.yeah in javerscript things work a bit diffrent,I didnt have any idea that i have to use IEnumerator in C# to make things like this
     
  5. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    I thought it was yield return, not just yield...?

    And everyone above is right. It needs to return IEnumerator, not void, and needs to be called with StartCoroutine().
     
  6. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    or

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class NewBehaviourScript1 : MonoBehaviour {
    4.     GameObject obiekt;
    5.     int xx ;
    6.     //bool aaa=true;
    7.     void Start () {
    8.       Invoke("waitt", 2.0f);
    9.     }
    10.  
    11.     void Update () {
    12.      //   if (aaa) {waitt();}
    13.      }
    14.  
    15. void waitt()
    16.     {
    17.       //  aaa=false;
    18.         //yield WaitForSeconds(2);
    19.     obiekt.SetActive(true);
    20. }
    21. }
    cranky was mostly right above as well... but for c# it is

    Code (CSharp):
    1.  yield return new WaitForSeconds(2.0f);