IEnumerator not working, HELP!

Updated on May 1, 2017 in  [R] Scripts
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
6 on April 30, 2017

Hey guys,

For some reason my IEnumerator method is not working, Please

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
public class ExampleMethod : MonoBehaviour{
void Update () {
 StartCoroutine (Example());
 }
 IEnumerator Example(){
 Debug.Log ("5 Seconds has passed!");
 yield return new WaitForSeconds(5.0f);
 }
 }
 

THANKS

  • Liked by
Reply
1 on April 30, 2017

For some reason i have no clue what your goddamn issue is.

on April 30, 2017

Exactly how i feel, lol

Show more replies
  • Liked by
Reply
Cancel
1 on April 30, 2017

Maybe u should put debug.log after waitforseconds?

on April 30, 2017

I’ve tried it already, still didn’t work DX.

Show more replies
  • Liked by
Reply
Cancel
0 on April 30, 2017

your code will print “5 Seconds has passed!” in every frame, and wait 5 seconds, and returns.
1.
you should place :
print “5 Seconds has passed!” 
after 
yield return new WaitForSeconds(5.0f);

2.
your doing this every frame, i think you want to wait 5 sec and do something, so place :
StartCoroutine (Example());
in start, instead of update.

3. if you want do something every 5 sec, you should place :
StartCoroutine (Example());
in start, and  end of Example function, so that it called itself after 5 sec.

final code:
1.do something after 5 sec:

using UnityEngine;
using System.Collections;
public class NewBehaviourScript2 : MonoBehaviour {
 // Use this for initialization
 void Start () {
 StartCoroutine (Example());
 }
 // Update is called once per frame
 void Update () {
 }
 IEnumerator Example(){
 yield return new WaitForSeconds(5.0f); 
 Debug.Log ("5 Seconds has passed!");
 }
}

2. do something every 5 sec:

using UnityEngine;
using System.Collections;
public class NewBehaviourScript2 : MonoBehaviour {
 // Use this for initialization
 void Start () {
 StartCoroutine (Example());
 }
 // Update is called once per frame
 void Update () {
 }
 IEnumerator Example(){
 yield return new WaitForSeconds(5.0f); 
 Debug.Log ("5 Seconds has passed!");
 StartCoroutine (Example());
 }
}

  • Liked by
Reply
Cancel
0 on May 1, 2017

Hey, thanks for all the replies, i’ve found an alternative.

  • Liked by
Reply
Cancel