Slowly change of color back and forth

Hi I want to make simple item highlighting but not just popup new color bot slowly change to new color when mouse is over item and when it goes away it back to original color.
I found something that somewhat works when you use keyboard shortcuts and give function time to finish but when i tap one key after another instead of slowly change to original color it just change to new color and popup to orginal just like in mouseover/exit works, it seems that IEnumerator work for changing to new color and orginal at the same time and when new color is finish it just pop up orginal that finish tiny sec after

What I want is to make that smart script that slowly increase color to new one but when interrupt it go back to original from that point and I have absolutely no idea how to do it :slight_smile:

Any help would be great :slight_smile:

and here is my code

using UnityEngine;
using System.Collections;

public class leanScaleandColor : MonoBehaviour {

	public Color[] c;
	Color startColor ;

	void Start () {
		startColor = transform.renderer.material.color;

	}
	
	void Update () {

				if (Input.GetKeyUp (KeyCode.T)) {
						StartCoroutine (FadeTo (c [0], 1.0f));
				}
				if (Input.GetKeyUp (KeyCode.F)) {
						StartCoroutine (FadeTo (startColor, 1.0f));

				}
		}

	
void OnMouseOver(){
		StartCoroutine(FadeTo(c[0] , 1.0f));
	}

void OnMouseExit(){
		StartCoroutine(FadeTo(startColor, 1.0f));
	}



IEnumerator FadeTo(Color NewColor, float aTime)
	{
				Color curentColor = transform.renderer.material.color;
				for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
		{
			Color newColor2 = Color.Lerp (curentColor, NewColor, t);
				transform.renderer.material.color = newColor2;
				yield return null;
		}
	}

}

This should do the work :wink:

using System.Collections;
using UnityEngine;

public class ColorFader : MonoBehaviour {

	public Color colorToApply;
	
	Color startColor;
	Color targetColor;

	bool coroutineRunning;

	void Start() {
		startColor = transform.renderer.material.color;
	}

	void Update() {
		if (Input.GetKeyUp(KeyCode.T)) {
			if (coroutineRunning  targetColor != colorToApply) {
				StopAllCoroutines();
			}
			StartCoroutine(FadeTo(colorToApply, 1.0f));
		}
		if (Input.GetKeyUp(KeyCode.F)) {
			if (coroutineRunning  targetColor != startColor) {
				StopAllCoroutines();
			}
			StartCoroutine(FadeTo(startColor, 1.0f));
		}
	}

	void OnMouseOver() {
		if (coroutineRunning  targetColor != colorToApply) {
			StopAllCoroutines();
		}
		StartCoroutine(FadeTo(colorToApply, 1.0f));
	}

	void OnMouseExit() {
		if (coroutineRunning  targetColor != startColor) {
			StopAllCoroutines();
		}
		StartCoroutine(FadeTo(startColor, 1.0f));
	}

	IEnumerator FadeTo(Color newColor, float aTime) {
		Color currentColor = transform.renderer.material.color;
		for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime) {
			coroutineRunning = true;
			targetColor = newColor;
			Color newColor2 = Color.Lerp(currentColor, newColor, t);
			transform.renderer.material.color = newColor2;
			yield return null;
		}
		coroutineRunning = false;
	}
}

Thanks Eugenio I will test it ans see if it works :slight_smile: meanwhile I made it work different way, finished like 10 min after Your answer :slight_smile:

now it would be great to have some easy in out :stuck_out_tongue:

       public Color col;
	Color startColor ;


	void Start () {
		startColor = transform.renderer.material.color;
		
		
	}
	

	void Update () {
		

		if (blendValue >0  mouseover == 0){
			blendValue -=  Time.deltaTime * 0.5f;
			transform.renderer.material.color =Color.Lerp (startColor, curentColor, blendValue);
			}
	}
	
	
	void OnMouseOver()
	{
		if (mouseover == 0) {
			curentColor = transform.renderer.material.color;
			blendValue =0;
		}
		mouseover = 1;
		if (blendValue < 1) {
			blendValue += Time.deltaTime * 0.5f;
			transform.renderer.material.color = Color.Lerp (curentColor, col, blendValue);
				}

	}
	
	
	void OnMouseExit()
	{
		if (mouseover == 1) {
			curentColor = transform.renderer.material.color;
		}
		mouseover = 0;
		blendValue =1;
	}