Assigning materials based on a Prefab

Hello!

I’m looking for some help with a very confusing problem. I have a top down game with an inventory and collection system. When a player hovers over an item, it turns either green or red based on whether it is within range to pick up or not. This works fine, but when I want to return the object to it’s original colors, I run into some problems. Here is my code:

function OnMouseExit ()
{
	var renderers = GetComponentsInChildren(Renderer);//get all the renderers in the object
	var children = thisPrefab.gameObject.GetComponentsInChildren.<Renderer>();//get all the original renderers
	var child : int = 0;
	for(var rend : Renderer in renderers)
	{
		for(var i=0; i < rend.materials.length; i++)
		{
			rend.materials_.color = children[child].renderer.materials*.color;*_

* print("child = " + child + " i= " + i);*
* }*
* child++;*
* }*
}
My print function, just for debugging, returns exactly what I would expect. For each object it returns exactly the number of materials on that object, but nothing changes at all. When the scene loads, the object is it’s own color, when I mouse over it changes to red/green, but when this fires nothing at all happens. I get no error message.
Anyone have any ideas that can point me in the right direction?
Thanks a lot!

What I’m doing for this is that I have a really simple behavior attached to anything inside of my prefab that includes a MeshRenderer - this component has a public List that matches per-index the materials that are set when the object Starts.

This allows me to revert every material (in position in the materials array) back to it’s original shader. This works but performance is not great (draw calls shoot up.)

I got it to work using something similar to what Thomas suggested. I feel it is a bit sloppy, but it gets the job done and only needs to be applied to the parent object.

var savedColors : Color[,] = new Color[10,10];
function Start()
{
    var renderers = this.gameObject.GetComponentsInChildren.<Renderer>();
	for(var j=0; j < renderers.length; j++)
	{
		for(var i=0; i < renderers[j].materials.Length; i++)
		{
			savedColors[j,i] = renderers[j].materials*.color;*
  •  }*
    
  • }*
    }
    function OnMouseExit ()
    {
    var renderers = this.gameObject.GetComponentsInChildren.();
    for(var j=0; j < renderers.length; j++)
    {
    for(var i=0; i < renderers[j].materials.Length; i++)
    {
    _ renderers[j].materials*.color = savedColors[j,i];_
    _
    }_
    _
    }_
    _
    }*_