Make something change color for a set amount of time

Hello, I am trying to create a game that involves the player being hit and damaged by enemies. However, since I’m a beginner to Unity’s C# library, I do not know how I can make it so that the player will change color for a set amount of time (in order to show that damage has been dealt). Would somebody mind giving me some advice on how to do this? Thanks!

Since I think that you want to wait just a few frames, you could do something like this:

int i = 0;
bool timeIt = false;
void Update () {
    if (beenHit){
        // change color
        timeIt = true;
    }
    if (timeIt)
        i++

    if (i==30) {
       // change color back
        i=0;
        timeIt = false;
    }
}

Or if you want to wait a set amount of seconds you can do something like this:

public class ExampleClass : MonoBehaviour {
	public float waitTime = 1;
	private float timeAtStart;
	private bool first = true;
	void Update () {
		if (first) {

			// Change the colour

			timeAtStart = Time.time;
			first = false;
		}
		if (first == false && Time.time > timeAtStart + waitTime) {

			// Change the colour back

			first = true;
		}
	}

}

Note: the float value waitTime can be smaller than 1, allowing for short wait times.

Hope this helps,
Awanonimik

Ok, so far I have been able to register it to change the color of the player when he is hit, but I still can’t figure out how to change him back.

Here’s the complete PlayerController script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour {

	public int moveSpeed;
	public int hitPoints;
	
	private Rigidbody rb;
	private bool playerControl = true;
        //The Boolean that detects if the player is hit or not
	private bool hitDetector = false;

	void Start () 
	{
		rb = GetComponent<Rigidbody>();
	}
 
	void Update () 
	{
		if (playerControl == true) 
		{
			float h = Input.GetAxis("Horizontal") * moveSpeed;
			float v = Input.GetAxis("Vertical") * moveSpeed;
			
			Vector3 vel = rb.velocity;
			vel.x = h;
			vel.z = v;
			rb.velocity = vel;
			
			if (hitPoints == 0)
			{
				GetComponent<Renderer>().material.color = Color.grey;
				playerControl = false;
			}
		}
                //This script changes the color
		if (hitDetector == true)
		{
			for (int i = 0; i <= 300; i = i + 1)
			{
				GetComponent<Renderer>().material.color = Color.yellow;
				
			}
		}
                //End of aforementioned script
	}
	
	void OnTriggerEnter (Collider other) 
	{
		if (other.gameObject.CompareTag("Enemy")) 
		{
			hitPoints = hitPoints - 1;
			hitDetector = true;
		}
		if (other.gameObject.CompareTag("Collectable"))
        {
            other.gameObject.SetActive (false);
        }
	}
}