- Home /
rollover scale script problem
I would like to have an object scale when the mouse is positioned over it. I can get it to scale, but I can't get the object to return to it's original size OnMouseExit. What am I doing wrong?
function OnMouseEnter () { renderer.material.color = Color.red; transform.localScale += transform.localScale*0.1; }
// Assigns a white color to the material // attached to this mesh.
function OnMouseExit () { renderer.material.color = Color.white; transform.localScale -= transform.localScale*0.1; }
The math you're using won't actually return the scale to its original value. However, the difference will be $$anonymous$$imal in this particular case, so I doubt that's the problem. As for what the problem might be, does the color get reset to white when you expect it to? Or is that not happening either?
Answer by Travis 18 · Mar 28, 2011 at 04:05 PM
You're object won't return to it's original size because you're performing an operation on the local scale after you've changed it. So if you local scale starts at 100, and you add ten percent you end up with 110. If you then subtract ten percent of THAT, you get 110 - 11 = 99.
It might be a negligible amount, but you can avoid it by saving the original value in the OnMouseEnter function and calling it later, like so:
var oldScale;
function OnMouseEnter(){ oldScale = transform.localScale; transform.localScale += transform.localScale.0.1; }
function OnMouseExit() { transform.localScale = oldScale; }
Your answer

Follow this Question
Related Questions
Scale of GameObject is reset when enable an attached Animator component 1 Answer
How to covert a rectTransform scale to world scale? 1 Answer
Enemy gameObject didn't move 1 Answer
Raycast won't work after Camera transforms position. Please help! 1 Answer
OnSceneGUI has odd behaviour with multiple objects selected 1 Answer