How to make incorporate a function and a loop

[SerializedField] Material materials = new Material[2];
Renderer_renderer;

private void Awake()
{
	 _renderer = GetComponent<Renderer>()
        _renderer.sharedMaterial = materials[0];
    
}

private void OnCollisisonEnter(Collision collision)
{
    if (collision.collider.CompareTag("User"))
        _renderer.sharedMaterial = materials[1];
    
}

private void OnCollisionEnter(Collisison collision)
{
    
}

void Color private void OnCanvasGroupChanged()
{
	 void ColorChange()
}
{
    Renderer renderer = GetComponent<Renderer>();

    //changes the colour to purple
    renderer.material.shader = Shader.Find("_Color");
    renderer.material.SetColor("_Color", Color.purple);

    //changes the colour to red
    renderer.material.shader = Shader.Find("Specular");
    renderer.material.SetColor("_SpecColor", Colour.red);

    //chnages the color to yellow
    renderer.material.shader = Shader.Find("_Color");
    renderer.material.SetColor("_Color", Color.yellow);

    //changes the color to cyan
    renderer.material.shader = Shader.Find("Specular");
    renderer.material.SetColor("_SpecColor", Color.cyan);
  • How can I add a function (not a built-in function) into my code?

Just make one?

public void ThisIsOneIMade() {
//Code to execute
}

then u can call it like

ThisIsOneIMade(); anywhere u want in another function

This is basic C#…

I think you missing something.

void Color private void OnCanvasGroupChanged()  *//I am not sure what are you talking about here*
 {
      ColorChange();       *//Remove void here and put a semicolon at the end.*
 }
public void ColorChange() {
     Renderer renderer = GetComponent<Renderer>();
     //changes the colour to purple
     renderer.material.shader = Shader.Find("_Color");
     renderer.material.SetColor("_Color", Color.purple);
     //changes the colour to red
     renderer.material.shader = Shader.Find("Specular");
     renderer.material.SetColor("_SpecColor", Colour.red);
     //chnages the color to yellow
     renderer.material.shader = Shader.Find("_Color");
     renderer.material.SetColor("_Color", Color.yellow);
     //changes the color to cyan
     renderer.material.shader = Shader.Find("Specular");
     renderer.material.SetColor("_SpecColor", Color.cyan);
}                                          *//You missed a basic syntax of a function*