Change Material Color on Prefab Instances

I’m just starting to learning unity, but generally feel comfortable with Javascript. I’m trying to write a script to that will randomly assign color’s to a field of (prefab) instance cars. I have successfully been able to this on non-prefab game objects (a sphere). But when I switched to my prefab car. Only the last instance changes. When I look in the inspector during playtime. The last clone has a NEW clone material attached, but all of the car’s don’t. I added in a debugging messages that show that each of my cloned prefabs have a unique instance ID, but when I use: car.Find(“body”), it always returns the same instance of the body (Same instanceID). Has anyone had similar trouble? I feel think there is something simple I must be overlooking. Below is the code.

Thanks
~HyFrmn

var car : Transform;

private var cameros = new Array();	

function Start(){	
	for (var j=0; j<5; j++){
		for (var i=0; i<5; i++){
			pos = Vector3(j * 15, 0, i * 40);
			new_car = Instantiate(car, pos, Quaternion.identity);
		}
	}
}

function OnGUI () {
	if (GUI.Button(Rect(10, 10, 100, 20), "Random Colors")){		
		cars = GameObject.FindGameObjectsWithTag("cars");
		for (var carGameObject : GameObject in cars){
			mat = carGameObject.Find("body").renderer.material;
			Debug.Log(carGameObject.GetInstanceID() + " " + carGameObject.Find("body").GetInstanceID() + " " + mat.name);
			mat.color = Color(Random.value, Random.value, Random.value);
		}	
	}	

}

[/code]

When you assign a color to a material, UT will create a new material for that object, prefab or not. If you wanna change the color of any object using the material you need to change the renderer.sharedMaterial property.
BTW using Find("body”) is a huge performance hog.

I just tried using the sharedMaterial property. It does work to change all the color of all the cars (as expected), but it also changes the material stored in my project (as documented), which is not what I want. Also this still doesn’t allow me to change color’s individually on cars.

I have also tried to Instantiate the original material and assign that back to the GameObject, but this again only creates one new material. Has anyone had luck doing something similar to this. Procedurally assigning different properties to material instance’s attached to GameObjects.

I have a system that does this. Here is how it works.

I have a GameObject(GO henceforth) named SpawnManager in my scene. Attached to that, I have a SpawnItem script that has a few public arrays that I can populate through the inspector. One of the arrays is “colors:Color[ ]”. This gives me an array of color swatches I can set for all my color values.

When I call the SpawnItem from my other scripts, I instantiate an object, determine which direction it is supposed to move and apply the appropriate mover script to the new object. The Awake() function of the mover script is where I pick the color from the array, and set that color on the instantiated clone. Here is my Awake() function:

function Awake(){
	var randCol:int = Random.Range(0, 19);
	var sm = GameObject.Find("SpawnManager");
	renderer.material.color = sm.GetComponent(SpawnItem).colors[randCol];
}

Hope this makes some sense. Let me know if you need clarification.