- Home /
How do I make a color lerp reset back to the original color after I click on a new tile?
I am making a game that utilizes a 10 x 10 grid. When the player clicks on a tile, the selected tile is supposed to slowly flash between black and green until a new tile is selected or the current tile is deselected. I achieved this effect using Color.Lerp, but the problem is when I select a new tile, the color lerp does not start over. It continues mid-cycle from the color from which it left off. For example, when I select a tile and let it fade to green then select a new tile, the new tile starts off at the same shade of green and the color lerp goes from there. I want it to start over from black. Here is my code:
public void OnMouseDown()
{
SelectNewTile();
}
private void Update()
{
if(tileSelected)
{
//Makes selected tile slowly blink green
float t = Mathf.PingPong(lerpTime, 1.0f) / 5.0f;
selectedTile.GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, t);
lerpTime += Time.deltaTime;
}
}
public void DeselectOldTile()
{
selectedTile.GetComponent<Renderer>().material.color = startColor;
tileSelected = false;
lerpTime = 0.0f;
}
public void SelectNewTile()
{
bool canMove = false;
if(!tileSelected)
{
selectedTile = transform;
currentTile = selectedTile;
tileSelected = true;
}
else if(selectedTile.Equals(transform))
{
DeselectOldTile();
tileSelected = false;
}
else
{
DeselectOldTile();
selectedTile = transform;
currentTile = selectedTile;
tileSelected = true;
}
if (selectedTile.childCount != 0)
{
canMove = true;
}
UpdateTileInfo(tileSelected, canMove);
}
I have searched everywhere for a solution and tried every possible fix that came to my mind, but nothing has worked. I even used print statements to debug it, and they printed out that when t = 0, the material color is indeed black, yet the tile was still green. I am out of options, so I will be beyond grateful for any help.
Your answer
![](https://koobas.hobune.stream/wayback/20220612130229im_/https://answers.unity.com/themes/thub/images/avi.jpg)