- Home /
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;
}
}
Answer by HalversonS · Apr 10, 2014 at 07:28 PM
Okay, so this works :P I didn't realize that I could check to see if there was a texture attached to a component. http://docs.unity3d.com/Documentation/ScriptReference/Material-mainTexture.html
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;
}
}
}
Answer by Mikael-Gyth · Apr 10, 2014 at 07:06 PM
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%)
Right, but the vast amount of items I have vary in such that I'd have to individually change each one. This could take days :P if not weeks. (I'm talking a ridiculous amount of objects)
I'm trying to get whatever script I'm running to be applied to any object that needs a mouseover highlight and have it function the same without adjusting any additional assets or components. It works great on gameobjects without children and gameobjects that have a texture on each renderer.
I just gave myself an idea with the way I phrased the last sentence ^ waiting for everything to build.
I'll keep what you posted in $$anonymous$$d for my next project. That way I can build from the ground up without having to go back and change a million things. Thanks $$anonymous$$ikael!
Your answer
Follow this Question
Related Questions
Material.SetColor and Material.color are not working properly 0 Answers
What is the best way to have different colored material on a batch of the same 3D object? 1 Answer
Specific colors change gradually as I keep playing on Android! 0 Answers
How to gradually transition between sprites? 1 Answer
Car Model Run time Color Change Problem 0 Answers