- Home /
Question by
prashant2000 · Oct 16, 2017 at 07:35 PM ·
unity 5waitforsecondscoroutine errors
WaitForSeconds working but going infinitely
i am new to coding... here is my code..
using UnityEngine; using System.Collections;
public class invib : MonoBehaviour {
// Use this for initialization
void Start () {
//Debug.Log("okay:");
StartCoroutine(invi());
//Debug.Log("okay:2");
}
IEnumerator invi()
{
yield return new WaitForSeconds(2);
Debug.Log(Time.time);
StopCoroutine(invi());
}
// Update is called once per frame
void Update () {
}
}
IT PRINTS FINE BUT... OUTPUT IS INFINTE.. 2.01 2.4 3.9 etc.. plz help
Comment
Answer by Stathis-Kap · Oct 16, 2017 at 09:41 PM
@prashant2000 I would recommend you to do something like this.
Coroutine inviRoutine = null;
void Start()
{
inviRoutine = StartCoroutine(invi());
}
IEnumerator invi()
{
yield return new WaitForSeconds(2);
Debug.Log(Time.time);
StopCoroutine(inviRoutine);
}
Credits: http://answers.unity3d.com/answers/934495/view.html
Your answer
Follow this Question
Related Questions
Wait for seconds not working second time 1 Answer
Coroutine sequence not running properly 1 Answer
Wait For Seconds Instantiate Inside For Loop 2 Answers
Coroutines WaitForSeconds – uneven spacing 2 Answers
WaitForSeconds Not Working 4 Answers