Search Unity

Action on Mouse Hover - SOLVED

Discussion in 'Scripting' started by radler470, Jun 8, 2015.

  1. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Super new to Unity, so bear with me.

    I am wondering what the easiest way is to have an object change when the mouse hovers over it. I tried doing a OnMouseOver, which worked fine except that I want the object to restore when the mouse isn't over it any more. Does this make sense?

    I know nothing about raycasting, so if there's a simpler way that would be preferred, but if not - please be specific. Thank you!
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Maybe you don't know of the OnMouseExit?
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ActionOnMouseOver : MonoBehaviour
    4. {
    5.     Material material;
    6.     Color defaultColor = Color.green;
    7.  
    8.     void Start()
    9.     {
    10.         material = new Material(Shader.Find("Standard"));
    11.         gameObject.GetComponent<Renderer>().material = material;
    12.         material.color = defaultColor;
    13.     }
    14.  
    15.     void OnMouseOver()
    16.     {
    17.         material.color = Color.red;
    18.     }
    19.  
    20.     void OnMouseExit()
    21.     {
    22.         material.color = defaultColor;
    23.     }
    24. }
     
    radler470 likes this.
  3. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Ohhhhh, ok I will try that. Thank you!
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    There is also OnMouseEnter in case you only want something ran once. OnMouseOver is called every frame, which might not be needed. In my code example I use OnMouseOver, but it would be more efficient to just use OnMouseEnter
     
    radler470 likes this.
  5. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Ok, so I tried it with OnMouseOver and OnMouseExit, and it works - except that it jitters like crazy - probably because it's evaluating every frame? Any ideas on how to get around this?
     
  6. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    Was this post before or after you saw my post about OnMouseEnter?
    If OnMouseEnter doesn't work, than I would need more info.
    I don't know what you mean about jitter. What is it that you are changing?
     
    radler470 likes this.
  7. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    I did try the OnMouseEnter and I'm getting the same issue. At this point I'm just swapping it out with another object (I just want to get the behavior working before I add anything else to it.) It's sort of flickering back and forth between the objects when the mouse is over it.
    Code (CSharp):
    1. public class onMouseOver : MonoBehaviour {
    2.    
    3.     public GameObject hover;
    4.    
    5.     void OnMouseOver () {
    6.             hover.SetActive (true);
    7.                 }
    8.  
    9.     void OnMouseExit () {
    10.                 hover.SetActive (false);
    11.         }
    12.        
    13.     }
    14.  
     
  8. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    OnMouseOver,Exit,Enter, activate when the mouse goes over the gameObject the script is on. Did you put this script on both of the objects? On my end, using the script you gave above gave expected results. I place a gameobject through the inspector into the public game object field. when I hover over the game object this script was on, the other game object is hidden. when I move my mouse off, the other game object reappears.
     
    radler470 likes this.
  9. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Ok, yea, it is working, it had something to do with the object I was using, but when I use a Cube it works fine. Thanks so much for your help!
     
    HiddenMonk likes this.