i am making a game in which i am invoking a function which create a game object again and again. i want to give a different color to each game object the code is:
#pragma strict
var delay: float=0.5f;
var sphere:GameObject;
function Start () {
InvokeRepeating("OnCollisionEnter",delay,delay);
}
function OnCollisionEnter() {
Instantiate(sphere,new Vector3(Random.Range(-1,2),-3,-12.78451),Quaternion.identity);
}
There are a few different ways to change color. The most common is to change the main color attribute of the material. Note that only a fraction of the shaders that ship with Unity have a main color attribute. The default diffuse shader is one that does have this attribute. If so then your code could be:
function OnCollisionEnter() {
var go = Instantiate(sphere,new Vector3(Random.Range(-1,2),-3,-12.78451),Quaternion.identity);
go.renderer.material.color = Color(Random.value, Random.value, Random.value);
}