Im doing this to create a random color for instantiated objects.
var colorChoices : Color[];
this.gameObject.renderer.material.color = colorChoices[Random.Range(0, (colorChoices.length))];
How would I get a reference to the color on collision for example if there is a collision with a red one? Im thinking the code below might be better suited if I created a array of the five main colors but Im not sure how to do that either?
function OnCollisionEnter(other : Collision)
{
if(other.gameObject.renderer.material.color == "red")
{
print("Collided with RED");
}
}
Im stumbling around on this and keep coming across posts that don’t relate which is why Im posting.
This is what a Color is; it’s not a string. Red is (1.0, 0.0, 0.0, 1.0).
–Eric
Though not the right syntax Im sure this will work, I changed the colorChoices to be a random number and broke it up a bit to be able to access it and get the color.
Bounce.js
var colorChoices : Color[];
var randNum : int;
randNum = Random.Range(0, 5);
this.gameObject.renderer.material.color = colorChoices[randNum];
print(randNum);
function OnCollisionEnter(other : Collision)
{
if(other.gameObject.GetComponent(Bounce).randNum == 0)//error this line?
{
print("red collision coming soon I hope?");
}
}
hmm… not sure why Im getting an error for the if statement, this should work? The script is attached to this object and others.
Im getting the print out but also getting the error anyone have any ideas why?
You’re trying to access the Bounce component for an object that doesn’t have one. You should check if the component is null before trying to use it. Some other things: the randNum = Random.Range etc. code should be inside a function (such as Start), and you should use the length of the array rather than hard-coding numbers like that.
–Eric
As always Eric your spot on!
Thank you my friend.