- Home /
The question is answered, right answer was accepted
yield return new WaitForSeconds () not working
Hey! For some reason when I put yield return new WaitForSeconds inside boolean it coroutine just ignored it. These are parts of script I'm working on.
private void InitFinal (int i) {
if (spells [i].isCast) {
if (!UIManager.instance.castBar.gameObject.activeSelf && spells [i].castCounter <= 0) {
spells [i].casting = true;
if (spells [i].castDone) {
spells [i].UseResource (transform.GetComponent<Entity> ());
spells [i].Wait ();
spells [i].Init ();
spells [i].InitEffect ();
spells [i].castDone = false;
}
}
else {
if (Input.anyKeyDown) {
StartCoroutine (MessageText.instance.ShowMessage ("Action is in progress.", 2, Color.red));
}
}
}
else {
spells [i].UseResource (transform.GetComponent<Entity> ());
spells [i].Wait ();
spells [i].Init ();
spells [i].InitEffect ();
}
}
private IEnumerator InitFinalRanged (int i) {
if (spells [i].isCast) {
if (!UIManager.instance.castBar.gameObject.activeSelf) {
spells [i].casting = true;
if (spells [i].castDone) {
spells [i].InitEffect ();
spells [i].UseResource (transform.GetComponent<Entity> ());
spells [i].Wait ();
float time = distance / spells [i].effect.GetComponent <SpellEffect> ().moveSpeed;
yield return new WaitForSeconds (time);
spells [i].Init ();
}
}
else {
if (Input.anyKeyDown) {
StartCoroutine (MessageText.instance.ShowMessage ("Action is in progress.", 2, Color.red));
}
}
}
else {
spells [i].InitEffect ();
spells [i].UseResource (transform.GetComponent<Entity> ());
spells [i].Wait ();
float time = distance / spells [i].effect.GetComponent <SpellEffect> ().moveSpeed;
yield return new WaitForSeconds (time);
spells [i].Init ();
}
}
When I toggle isCast, InitFinalRanged coroutine just ignores that yield, but works perfectly when isCast = false.
Any ideas? Thanks!
Is the yield return new WaitForSeconds (time);
line even being reached? I'm guessing not, so one of your if statements is not evaluating to true. Use your debugger and step through the codes execution or put in some debug statements to see what actually gets executed.
Yeah, I fixed it, now it has time, but still ignores yield. Now it looks like this:
private IEnumerator InitFinalRanged (int i) {
float time1 = distance / spells [i].effect.GetComponent <SpellEffect> ().moveSpeed;
if (spells [i].isCast) {
if (!UI$$anonymous$$anager.instance.castBar.gameObject.activeSelf) {
spells [i].casting = true;
if (spells [i].castDone) {
spells [i].UseResource (transform.GetComponent<Entity> ());
spells [i].Wait ();
spells [i].InitEffect ();
yield return new WaitForSeconds (time1);
spells [i].Init ();
}
}
else {
if (Input.any$$anonymous$$eyDown) {
StartCoroutine ($$anonymous$$essageText.instance.Show$$anonymous$$essage ("Action is in progress.", 2, Color.red));
}
}
}
else {
spells [i].UseResource (transform.GetComponent<Entity> ());
spells [i].Wait ();
spells [i].InitEffect ();
float time2 = distance / spells [i].effect.GetComponent <SpellEffect> ().moveSpeed;
yield return new WaitForSeconds (time2);
spells [i].Init ();
}
}
Could distance be zero? If so, it would pass a zero in as the time to wait and return immediately.
Checked. Distance is not 0. As I said, it ignores yield only when isCast is toggled.
if (Ranged (spells [i])) {
if (Player.instance.selectedEntity != null) {
if (distance <= spells[i].range) {
if (!spells [i].waiting) {
if (spells [i].CanInit (transform.GetComponent <Entity> ())) {
if (direction > 0) {
StartCoroutine (InitFinalRanged (i));
}
else {
StartCoroutine ($$anonymous$$essageText.instance.Show$$anonymous$$essage ("You are facing the wrong way!", 2, Color.red));
}
}
else {
StartCoroutine ($$anonymous$$essageText.instance.Show$$anonymous$$essage ("No enough " + spells[i].resourceType.ToString(), 2, Color.red));
}
}
else {
StartCoroutine ($$anonymous$$essageText.instance.Show$$anonymous$$essage (spells[i].spellName + " is not ready yet!", 2, Color.red));
}
}
else {
StartCoroutine ($$anonymous$$essageText.instance.Show$$anonymous$$essage ("Enemy is not in range.", 2, Color.red));
}
}
else {
StartCoroutine ($$anonymous$$essageText.instance.Show$$anonymous$$essage ("No target selected.", 2, Color.red));
}
}
Answer by PAHeartBeat · Feb 11, 2017 at 09:15 AM
Hi,
WaitForSeconds is only not work in case, Time.deltaTime must be zero. Please check Time.deltaTime value.
WaitForSeconds depends on deltaTime.
Hey! I tried to print Time.deltaTime like this:
if (spells [i].isCast) {
if (!UI$$anonymous$$anager.instance.castBar.gameObject.activeSelf) {
spells [i].casting = true;
if (spells [i].castDone) {
spells [i].UseResource (transform.GetComponent<Entity> ());
spells [i].Wait ();
spells [i].InitEffect ();
print (Time.deltaTime);
yield return new WaitForSeconds (time1);
print (Time.deltaTime);
spells [i].Init ();
}
}
But it never showed in console. Any ideas?
If it never printed then one of your it statement is not true.
But how ti calls Init () then? It's right below yeild.
As @jaden300 maintian, there are no true condition for you 3 condition, cos there 3 if statement based on spells[i] and try print time delta time before spells[i].isCast
EDIT: also check for time not goes to infinite value. there are only very few chance to time value goes to infinite. but check once.
Okay, just printed before and after every bool, and checked, castDone is always false, but I tried printing castDone in InitFinal () function (which works perfectly) and it didn't even printed.
For some reason it just doesn't Work inside Coroutine.