title: Creating a color loop effect in Unity?
body: <p>I'm currently developing a game in Unity and I ran into a small problem. I'm trying to generate a rainbow color effect that is similar to what happens when someone catches a star in mario kart or supermario on a specific gameobject. Essentially all the instances of the prefab attached to the script InteractControl that are active should get this effect (Either loop or lerp trough 5 different colors continusesly) while the variable timeLeft > 0 and the score > 10 and then go back to the original value of the color they had when timeLeft &lt; 0.</p>
  
  <p>Ive already tried multiple things including using a coroutine, and reseting the color inisde of it multiple times. Creating a new method and allowing acces to it only if a bool whom's value is set within the timeleft > 0 if statement is true, or currently trying to lerp the color however this all doesnt seem the work. Trying to make an animation could also be an option however its too complicated to make the objects of different colours match the same dimensions as the original I have so I really want to make it change trough the script.</p>
  
  <p>These are the relevant parts of the script in question:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class InteractControl : MonoBehaviour, IPooledObject
  {
      public Transform[] spawnPoints;
      public Color[] _colors;
      private float t = 0.2f;
      private Rigidbody2D rb;
      GameObject target;
      Vector3 directionToTarget;
      public static int LevelStart = 0;
      public static float moveSpeed = 5f;
      public static bool isSlowMotion = true;
      public static float timeLeft = 12f;
      private float randomSpawnTime;
      private int Spawn = 1;
      private float scaledTime;
      private Color oldColor;
      private Color newColor;
      private float newT;
  
      private void Start()
      {
          scaledTime = Mathf.Sin(t) * (float)(5 - 1);
          oldColor = _colors[(int)scaledTime];
          newColor = _colors[(int)(scaledTime + 1f)];
          newT = scaledTime - Mathf.Round(scaledTime);
      }
  
  
  
   public void OnObjectSpawn()
      {
  
          target = GameObject.FindWithTag("White Ball");
          rb = GetComponent&lt;Rigidbody2D&gt;();
  
      }
  
      void Update() 
      {
          if (target != null)
          {
              if (ScoreScript.scoreValue &gt; 10)
              {
                  timeLeft -= Time.deltaTime;
  
                  if (timeLeft &gt; 0)
                  {
                      directionToTarget = (target.transform.position - transform.position).normalized;
                      rb.velocity = new Vector2(directionToTarget.x * moveSpeed,
                                                  directionToTarget.y * moveSpeed);
  
  
  
                      GetComponent&lt;Renderer&gt;().material.color = Color.Lerp(oldColor, newColor, 5f);
  
                      if (Spawn == 1)
                      {
                          randomSpawnTime = Random.Range(2, 8);
                          Spawn++;
                      }
  
                      if (timeLeft &lt; randomSpawnTime &amp;&amp; Spawn &lt;= 2)
                      {
                          BallSpawnerControl.ClockSpawn = true;
                      }
  
                  }
  
                  if (timeLeft &lt; 0)
                  {
                      GameObject[] gos = GameObject.FindGameObjectsWithTag("ColouredBall Highress");
  
                      foreach (GameObject go in gos)
                      {
                          go.SetActive(false);
                      }
  
                      BallSpawnerControl.HeartSpawn = true;
  
  
                  }
              }
  
          }
  
      }
  }
  </code></pre>