- Home /
Question by
Metalmayhem · Mar 24, 2015 at 06:05 AM ·
renderertagsonmouseover
Change Object Color Using Tags OnMouseOver
I am attempting to change my object's Color when Mouse Moves over it, then go back to Default.
So far I have Material "Default", Material "Enemy", and the GameObject "cube" just to sample the code.
Code I've previously attempted:
//
var mat : Enemy;
function OnMouseOver()
{
if (Tag = GameObject.FindGameObjectWithTag("Enemy")
{
renderer.material = mat;
}
}
Comment
Answer by Metalmayhem · Mar 31, 2015 at 10:20 PM
Found the answer:
//linked created Material to variable in script
var defaultMat : Material;
//linked created Material to variable in script
var enemyMat : Material;
//What happens when the mouse moves over object with script attached.
function OnMouseOver ()
{
if (gameObject.CompareTag("Enemy"))
{
gameObject.GetComponent.<Renderer>().material = enemyMat;
}
}
//What happens when mouse moves off of object with script attached.
function OnMouseExit ()
{
gameObject.GetComponent.<Renderer>().material = defaultMat;
}