Excluding certain children from a loop (changing parented children's colors on mouseover)

My script is looking at each child component in a parent and saving the original color during Start. It is then, during mouseover, multiplying the color value of each component’s material (this is to give it a ‘highlight’ appearance).

Then, OnMouseExit(), it is supposed to return the color to the original. However all children components are returning blue. There is a child in the parent that doesn’t use a .png and instead is using a material with the Main Color set to a blueish hue.

I was able to get them to return to the right color… however the water then takes uses their color.

There are many gameobjects like this and I’d rather not tweak each one or create a unique script for each one…

Here is my current code:

#pragma strict

 
 var highlightValue = 1.50;
 var originalColor = Color();
 var children : Renderer[];
 
function Start()
{
   children = GetComponentsInChildren.<Renderer>();
   
   for(var i : Renderer in children)
   {
    originalColor = i.material.color;
   }
}
 
function OnMouseOver()
{

 for(var i : Renderer in children)
   {
     var wasLit : boolean = false;
     
   	 if (wasLit == false)
      {
       i.material.color = originalColor*highlightValue;
       wasLit = true;
      }
   }
}

function OnMouseExit()
{
 for(var i : Renderer in children)
  { 
    i.material.color = originalColor;   
  }
}

In your start method you iterate over all objects in children and set originalColor to the color of the object. originalColor is going to end up beeing the color of the last item in the collection every time.

in OnMouseExit you set all the objects colors to originalColor (the same color as the last object in the collection).

If the objects have different colors you will need to store the original colors in a collections too. That way you can set the “original” originalColor in OnMouseExit.

`

var originalColor : Color[]

`

and
`

var index : int = 0;
for(var i : Renderer in children)
  { 
    i.material.color = originalColor[index];
index++;
  }

`

For exclusion of objects I’d go with tags and only store the color of objects with a certian tag.

(PS. I’m not too good with UnityScript so the code might not be 100%)

Okay, so this works :stuck_out_tongue: I didn’t realize that I could check to see if there was a texture attached to a component.

Added a couple if statements. Every child in a parent highlights now if it has a texture.

#pragma strict

 
 var highlightValue = 1.50;
 var originalColor = Color();
 var children : Renderer[];
 
function Start()
{
   children = GetComponentsInChildren.<Renderer>();
   
   for(var i : Renderer in children)
   {
   if(i.material.mainTexture != null)
    {
    originalColor = i.material.color;
    }
   }
}
 
function OnMouseOver()
{

 for(var i : Renderer in children)
   {
     var wasLit : boolean = false;
     
   	 if (wasLit == false && i.material.mainTexture != null)
      {
       i.material.color = originalColor*highlightValue;
       wasLit = true;
      }
   }
}

function OnMouseExit()
{
 for(var i : Renderer in children)
  {
  if(i.material.mainTexture != null)
   {
    i.material.color = originalColor;
   }
  }
}