array with a list of materials

hi :slight_smile:
i need to build one array with a list of materials and assign one of them randomly to one gameobject, but i dunno the code for make an array of materials, any suggestion would be really appreciated, i’m just a newbye.
And sry for my english, i understand it but i suck to write in eng

Here’s one in C# that will set a material at start up, I would think lists would be better?

using UnityEngine;
using System.Collections;


public class arrayDemo : MonoBehaviour
{
	System.Random random = new System.Random();
	
	// Set the materials in the inspector
	public Material[] myMaterials = new Material[5];

	// Use this for initialization
	void Start ()
	{
		// Assigns a random material at start
		gameObject.renderer.material = myMaterials[random.Next(0,myMaterials.Length)];
	}
}

Here’s a one written in Unityscript with some possible uses

#pragma strict

// If you have a set amount of materials using arrays will be fine, but if you need to resize on the fly use lists
// Dont forget to assign the materials in the inspector
public var myMaterials : Material[];
var random = new System.Random();

// Using lists
public var myMaterialList = new System.Collections.Generic.List.<Material>();

function Start ()
{
	ChooseRandomMaterial ()
}

// Could call this function from somewhere else to change the material
function ChooseRandomMaterial ()
{
	//Array
	gameObject.renderer.material = myMaterials[random.Next(0,myMaterials.Length)];
	
	//List
	gameObject.renderer.material = myMaterialList.Item[random.Next(0,myMaterialList.Count)];
}

//Could call this function from somewhere else to remove a material from the list
function RemoveMaterial()
{
	//List only

	// Remove random material
	myMaterialList.RemoveAt(random.Next(0,myMaterialList.Count));
}

function RemoveMaterial( removeMe : Material)
{
	//List only
	
	// Remove by name
	myMaterialList.Remove(removeMe);
}

//Could call this function from somewhere else to and pass a new material into the list. 
function AddNewMaterial( myNewMaterial : Material )
{
	//List only
	myMaterialList.Add(myNewMaterial);
}

Try out both so you know how to use them, but choose one or the other for your project.

Accessing arrays

//Define a list using Javascript
var myList = new List.();
var anotherList = new List.();

//Define a list using C#
List<int> myList = new List<int>();
List<SomeClass> anotherList = new List<SomeClass>();

Brought to you by UnityGems

// Using lists
21899-header.jpg
instead use this…
Add namespace:
using System.Collections.Generic;

and in the class declare like this
21900-material.jpg

If I want to assign list of material from 1st to n, which code I should use for Unity C#?