Random Material in specific multiple materials element


This is bugging me for a while now.
How can i load in a random Material on Start in the second element of my materials?

The gameObject has 3 materials, in the renderer slots under materials.

There is simply no way that I can target the last or any specific array element and pass a random material to it.
The code snippet is attached to the gameobject which has a meshrenderer. And the materials are dragged in the slot.

I need this because my gameobjects have different uv mapped materials.
When I press play I can see my material in the second slot is still the same, but it has ( instanciated ) behind it.

Aka no random material, How can i make this work please?

void Start ()  {
		rend = GetComponent<Renderer>();

		int randomInt = Random.Range (0, randomMaterials.Length);

		//rend.sharedMaterials[ 2 ] = randomMaterials [randomInt];

		int lng = rend.materials.Length;

		int range = gameObject.GetComponent<Renderer>().materials.Length;
		//print ("range = " + range);
		for (int m = 0; m < range; m++)

			if(m == 2){
				gameObject.GetComponent<Renderer>().materials[m] = randomMaterials[ randomInt ];
				print ("m = " + m + " range = " + range + " randomInt " + randomInt);

Many thanks

You need a temp variable of materials:

Material[] mats =gameObject.GetComponent<Renderer>().materials;
mats[2] = randomMaterials[ randomInt ];

// if you want to refresh materials, you need assign the new materials array:
gameObject.GetComponent<Renderer>().materials = mats;