Using a coroutine to delay the changing of a material color

Hi guys

I’d like to use a collision to change the colour of a material on 3 meshes consecutively. So:
Collision occurs and mesh 1 changes colour instantly
Then after 0.5s the second mesh changes colour
Then after 1s the third meshs changes colour

From what I’ve read I need to use a coroutine. I’ve tried scripting from looking at other sources but no luck so far. Inexperienced scripter so prepare to be astounded with my attempt!

Abbreviated below

public material glow;
public float waitTime1 = 30f;


	void OnTriggerEnter (Collider col) 
	{
		if (col.collider.tag == "Impulse") 
		{
			impOpticARenderer.renderer.enabled = false;
			impOpticBRenderer.renderer.enabled = false;
			diencephalonRRenderer.renderer.material = glow;
			hemibrainRightRenderer.renderer.material = glow;
			StartCoroutine(SequentialRenderers());
		}
	}


	IEnumerator SequentialRenderers()
	{
		yield return new WaitForSeconds(waitTime1);
		cerebellumLeftRenderer.renderer.material = glow;
	}

Any help appreciated

This is pretty good so far, what are you expecting it to do that it isn’t doing?

Try adding another

yeild return new WaitForSeconds(waitTime1);
otherObject.renderer.material = glow;

for each thing you want to change.

May be this could help you

ah ok, how are you triggering the material change function?

adding this simple function to a cube works so maybe its your collider function.

public Material glow;
	public float waitTime1 = 5f;
	void OnMouseOver()
	{
		Debug.Log( "OK" );
		StartCoroutine(SequentialRenderers());
	}
	IEnumerator SequentialRenderers()
	{
		Debug.Log( "TRIG" );
		yield return new WaitForSeconds(waitTime1);
		this.renderer.material = glow;
	}

a couple more things, im guess the lower case

public material glow;

is a typo because it should be

public Material glow;

also your adding material to slot in the inspector.

im not an advanced coder and still trying to learn myself.

I’m lost then, if you are able to send me the scene i could have a better look but the code you’ve shown looks fine,
sorry i could’nt be more help.