How to assign a random material to objects while i pick it up

I have 3 objects in my scene and 3 materials, what I would like is for every time i pick up object, the objects will have a random material based on the 3 materials. Like i picked up one item,and i want that after this action my other items also change color. I have this script so far,but only one item change color while i pick it up

public GameObject[] arrayofGameobjects;
public Material[] arrayofMaterials;
private MeshRenderer render;

private void Start()
{
    render = GetComponent<MeshRenderer>();
}


public void OnTriggerEnter(Collider other)
{
    foreach(GameObject _object in arrayofGameobjects)
    {
        render.material = arrayofMaterials[Random.Range(0, arrayofMaterials.Length)];
    }
}

Here you are assigning the material to render 3 times in a row…
Simply replace render by _object.GetComponent<MeshRenderer>() in the foreach loop and it should work :slight_smile: