animated sprite coroutine problem

using UnityEngine;
using System.Collections;

public class walkAnimator : MonoBehaviour {

public string characterStatus;
public int animationStatus;

public float idleFPS;
private float idleSecondsToWait;
public bool idleLoop;
public Texture[] idleFrames;

private int idleCurrentFrame;
	
public float walkFPS;
private float walkSecondsToWait;
public bool walkLoop;
public Texture[] walkFrames;
private int walkCurrentFrame;


// Use this for initialization
void Start () 
{
	idleCurrentFrame = 0;
	idleSecondsToWait = 1/idleFPS;
	walkCurrentFrame = 0;
	walkSecondsToWait = 1/walkFPS;
	characterStatus = "Walk";
	animationStatus = 1;

}
void Update () 
{
	if (animationStatus == 1)
	{
		if 	(characterStatus== "Idle")
		{
	
			StartCoroutine(idle());
			
		}
		if 	(characterStatus== "Walk")
		{
			StartCoroutine(walk());
			
		}
	}
	if (animationStatus ==2)
	{
	
	}
	
}

IEnumerator idle()
{
	animationStatus = 2;
	bool stop = false;
	
	if(idleCurrentFrame >= idleFrames.Length)
	{
		if(idleLoop == false)
			stop = true;
		else
			idleCurrentFrame = 0;
		Debug.Log("loop");
	}
	
	yield return new WaitForSeconds(idleSecondsToWait);
	
	renderer.material.mainTexture = idleFrames[idleCurrentFrame];
	idleCurrentFrame++;
	
	if(stop == false)
		StartCoroutine(idle());
	if 	(characterStatus== "Walk")
		{
			StartCoroutine(walk());
			StopCoroutine("idle");
			
		}
}
IEnumerator walk()
{
	bool stop = false;
	animationStatus = 2;
	
	if(walkCurrentFrame >= walkFrames.Length)
	{
		if(walkLoop == false)
			stop = true;
		else
			walkCurrentFrame = 0;
			Debug.Log("loop");
	}
	
	yield return new WaitForSeconds(walkSecondsToWait);
	
	renderer.material.mainTexture = walkFrames[walkCurrentFrame];
	walkCurrentFrame++;
	
	if(stop == false)
		StartCoroutine(walk());
	if 	(characterStatus== "Idle")
		{
	
			StartCoroutine(idle());
			StopCoroutine("walk");
			
		}
}

}

My issue seems to come after the initial load. The script passes into the walk coroutine with no trouble but if I chance the characterStatus string to walk in an attempt to swap animations the script starts stacking the coroutine over and over instead of running through it in a loop.

Well, I code some simple sprite animation class days later, was something like:

  [Serializable.Attribute]
    public class Sprite () {
    
    public Texture2D[] Frames;
    private Texture2D currentFrame;
    
    public IEnumerator animate(float frameDelay, bool shouldLoop) {
    
    while(true) {
    
    for(int i = 0; i < Frames.Length; i++) {
    
    currentFrame = Frames*;*

yield return new WaitForSeconds(frameDelay);

if((i == Frames.Length - 1) && (shouldLoop)){

i = 0;

}
}
}
}

public Texture2D getCurrentFrame {

get { return currentFrame; }

}

}
And you can call it like so :
public class spriteImplementation: Monobehaviour {

public Sprite walk; // you can add the sprite frames directly in the inspector

void Start () {

StartCoroutine(walk.animate(0.1F, true));

}

void Update () {

transform.renderer.material.mainTexture = walk.getCurrentFrame;

}

}
I don’t know if this can be helpful in your case, but I hope so.
PS:. I didn’t test this code, hope there aren’t syntax errors.

Ok so you are starting coroutines all over the place!! :slight_smile:

Normally you wouldn’t chain coroutines like you are for idle - a loop and a yield would achieve that for you. When you wanted to start walk then you would start that coroutine and just let the idle one fall out of the loop and return.

But then you are starting the coroutines in Update too - I guess that is ok because of your animationState change stopping continual repetition - but then you dont need to start coroutines in update, you could just start idle() in Start and remove that added complexity.