- Home /
Coroutine exiting unexpected
Hey Guys,
So here is my problem. I have a coroutine which is stopped, without even being completed, I wonder is some one has an issue with this. It isnt stopped because there is only 1 StopCoroutine call in my entire project and it is not stopping the problematic function. Also it is not the one calling the coroutine which is the problem because it is a singleton so it is never destroyed. Wonder if some of you might have encountered this issue. Here is the code of my coroutine.
IEnumerator MoveTroops(BaseCell aSource, BaseCell aTarget, Troops aTroops, float aTime)
{
float lerpTime = 0;
bool mLabelAnimPlayed = false;
Link link = aSource.GetLink(aTarget);
Vector3 troopsStartPos = Vector3.zero;
Vector3 troopsEndPos = Vector3.zero;
Vector3 startPos = Vector3.zero;
Vector3 endPos = Vector3.zero;
//
while(lerpTime < aTime)
{
if(link == null)
{
yield break;
}
//
lerpTime += Time.deltaTime*PlayerData.Instance.MovementSpeed;
link.Value = lerpTime/aTime;
//
troopsStartPos.Set(aSource.transform.position.x, aSource.transform.position.y, -2);
troopsEndPos.Set(aTarget.transform.position.x, aTarget.transform.position.y, -2);
aTroops.transform.position = Vector3.Lerp(troopsStartPos, troopsEndPos, lerpTime/aTime);
//
if((aTarget.Owner != PlayerData.Instance.Owner) && (aTarget.CurrentState == ECellState.Idle))
{
if(lerpTime > (0.5f*aTime) && !mLabelAnimPlayed)
{
startPos.Set(aTroops.transform.position.x, aTroops.transform.position.y, 0);
endPos.Set(aTarget.transform.position.x, aTarget.transform.position.y, 0);
float dist = Vector2.Distance(startPos,endPos);
if(dist <= START_FIGHT_MODE_DISTANCE)
{
aTarget.MoveLabelUp();
mLabelAnimPlayed = true;
}
}
}
Debug.Log("GOING TO"+lerpTime);
yield return new WaitForEndOfFrame();
Debug.Log("Coming BACK"+lerpTime+ " < "+aTime);
}
//
if(aTarget.Owner == PlayerData.Instance.Owner)
{
aTarget.NumberOfTroops += aTroops.Count;
aTarget.RemoveTroop(aTroops);
}
else
{
//TODO: Check if its me who's attacking.
if(aTarget.CurrentState == ECellState.Idle)
{
StartCoroutine(Fight(aSource, aTarget, aTroops));
}
//
aTarget.AddTroops(aTroops);
}
Debug.Log("ending");
}
The debug "ending" never gets call and the "coming back" debug shows that the condition is still true so it should continue, but it seems to stops for no reason. Any ideas ?
Thanks a lot for your time !
Claude
Yes you are right, this was it, I was too tired after a entire day of program$$anonymous$$g and couldn't see it. Good catch my friend :)
Thanks for taking the time to mark it as the answer - that really made my day yesterday!
Answer by karlhulme · Oct 27, 2013 at 07:32 AM
Is it possible for 'link' to become equal to null while the coroutine is running. This would explain the debug output you're seeing as you have a yield break on that condition.