- Home /
How would I change the colour of a prefab relative to the size of the character?
What I'm asking is how would I change the colour of an object relative to the size of the player?
What I'm trying to achieve is allowing the player to know whether what he is trying to "eat" is small enough to do so, however I'm having trouble determining whether something is of the right size.
I thought up a way to let the player know by having the enemy prefabs change in colour if the player's size is large enough to consume it.
How would I achieve this? The current player-growth-in-size script is as follows;
else if (other.tag == "Roach") { if(transform.localScale.magnitude<5) { transform.localScale += Vector3(.35,.35,.35); cam.GetComponent(SmoothFollow).Zoom(.7); cam.GetComponent(SmoothFollow).Height1(.4); jump.enabled=true;
}
}
Would I use a statement such as:
if(other.transform.localScale.magnitude < transform.localScale.magnitude)
?
Answer by karl_ · Jul 18, 2011 at 04:27 PM
Your best bet would be to access the material and adjust the color values. Alternatively, you could just switch the materials once their scale is appropriately small. Either way works, but I've included a snippet of code for adjusting color values:
if(other.transform.localScale.magnitude < transform.localScale.magnitude)
{
// Access the renderer attached to the enemy object,
// then set the main color to a color of your preference.
// You can also get more specific and toy with the ARGB values.
renderer.material.color = Color.green;
}
Useful links: http://unity3d.com/support/documentation/ScriptReference/Renderer-material.html
http://unity3d.com/support/documentation/ScriptReference/Color.html