- Home /
Changing the renderer.material.color of the children OnCollisionEnter?
First of all, hello all and thanks for reading! :)
I'd like to have my enemy flash red for a brief duration of time, when my spells hit it and then have it go back to its original color.
So far, after numerous tries and hours of research, I've got some result with the method posted [here][1] but it only changed one part of my character at time, and it did not work or look good over all. Not to mention I haven't found a way to get it back to its default look after an x period of time :(
Here's my code:
var children : Renderer[];
function Start()
{
children = GetComponentsInChildren.<Renderer>();
}
function OnCollisionEnter (collision : Collision)
{
if(collision.gameObject.tag == "Spell")
{
for(var i : Renderer in children)
{
children.material.color = Color.red;
yield WaitForSeconds(.5);
print("CHANGED COLORS?");
}
}
}
Now however, I can't even get the script running because it has been accusing the error: BCE0019 : 'children' is not a member of 'UnityEngine.GameObject'.
Is there any way to make this work?
Thank you all in advance! [1]: http://answers.unity3d.com/questions/64121/how-to-access-child-object-colours.html
@Chronos-L addressed your compiler error. Beyond that are you sure that all of your materials for all the children have a color attribute?
.............. I swear I thought I had used this before.
If I say I am very tired, will you believe me? LOL
THAN$$anonymous$$ YOU! :)
However, I still can't figure out how to change the color back. I tried it with i.material.color = Color.clear;
but it only turns it all black, or well, transparent. And I can't use 'null' either. Got any pointers, maybe?
You can store the original colour in a variable before changing it.
var original_color = i.material.color;
robertbu - the red tint seems to work fine, so I guess that means they do have the color attribute.... right? :<
merry_christmas - I guess I stand corrected, since when I tried to store the original color, it returned to me the following: "$$anonymous$$aterial doesn't have a color property '_Color'"
Sighs
What to do? :( And sorry if I am so helpless, guys. I did try to solve this myself, but I couldn't seem to find a solution for this at all...
Edit: typos haunt me.
Answer by Chronos-L · May 10, 2013 at 01:23 AM
for(var i : Renderer in children)
{
i.material.color = Color.red;
yield WaitForSeconds(.5);
print("CHANGED COLORS?");
}
Your answer
Follow this Question
Related Questions
how to make object stay destroyed after OnTriggerEnter? 1 Answer
JS Script only work in Editor 0 Answers
Change gameobject sprite with button 1 Answer