- Home /
Why isn't this IEnumerator method being called?
I have the following script attached to a Sphere game object. When I run the game, the Sphere begins ascending and never stops. The truth value of itemBounceUp never changes. It seems that itemBounce() is never getting called, but I have no error messages and I can't figure out why. I thought it might be the for-loop but I tried pulling it out to see if I could get itemBounce() to run though even once and the same thing happened.
Thanks, Billy
using UnityEngine;
using System.Collections;
public class BounceScript : MonoBehaviour {
public float bouncespeed = 0.002f;
public bool itemBounceUp = true;
public void Start() {
itemBounce();
}
public void Update () {
if(itemBounceUp == true) {
this.transform.position = this.transform.position + new Vector3(0, bouncespeed, 0);
}
else if(itemBounceUp == false) {
this.transform.position = this.transform.position + new Vector3(0, -bouncespeed, 0);
}
}
public IEnumerator itemBounce () {
for(int i=1;i>0;i++) {
yield return new WaitForSeconds(0.2f);
itemBounceUp = false;
yield return new WaitForSeconds(0.2f);
itemBounceUp = true;
}
}
}
Answer by Landern · May 06, 2014 at 06:42 PM
change:
public void Start() {
itemBounce();
}
to use a coroutine:
public void Start() {
StartCoroutine(itemBounce());
}
Your answer
Follow this Question
Related Questions
Understanding yield inside a for or a while loop 2 Answers
no "pointers" to get directly to a variable passed to a function? 1 Answer
Getting an error when trying to yield 2 Answers
is there any function call will be call after i activate an object ? 1 Answer
Yield Not Working - While Loop 3 Answers