OnMouseOver & OnMouseEnter dont work?

(In C#)

im trying to set my game at the moment so when the mouse hovers over a block, it will change the colour, essentially highlighting it
No matter what I try, making sure what the mouse hits is a block and nothing else, it wont change or even debug.log

Examples of what i have tried and nothing has worked:

void OnMouseOver(){
               if (GameObject.Find ("Manager").GetComponent<Manager> ().hasClicked == false) {
            
                        Debug.Log ("Over");
           }
}
void OnMouseOver(){

                        Debug.Log ("Over");
}
void OnMouseOver(){
          
                        Debug.Log ("Over");
}
void OnMouseEnter(){
               if (GameObject.Find ("Manager").GetComponent<Manager> ().hasClicked == false) {
            
                        Debug.Log ("Enter");
           }
}
void OnMouseEnter(){
                if (hitInfo.collider.tag == "Block") {
                        Debug.Log ("Enter");
       }
}
void OnMouseEnter(){
          
                        Debug.Log ("Enter");
}

Does your object have a collider ? And did you make sure it wasn’t in the ignore raycast layer ?

Yeah iv made sure the prefab is a box collider and i dont think its in the ignore layer, how do i know?

top of inspector.

other check to do, did you actually add the component to the object ?

And what is your build target, i don’t think this works if you target mobile.

Oh i see, its default layer not ignore and yes iv added it to my main camera, i know the main script works as its worked with a mouse click

If the script is added to the camera, then it will work fine… as long as you are hovering the mouse over the camera :eyes:

so your saying i need to add this script onto my blocks?
But in the same script attached to the camera i can click and change a blocks colour, so that doesnt make sense

so its working or its not working

what i have described, trying to get it to colour a block as a highlight only when the mouse is on it, not clicked, isnt working
while clicking a block changes a colour, so i dont see why one works and the other doesnt

Neither do we without code
Obviously you are doing something different for that

This is the code for clicking a block and changing colour, please look at this and help me get the mouseover or mouseenter to work in a similar way

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            ray = camera.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hitInfo))
            {
                if (hitInfo.collider.tag == "Block")
                {
                    if (selectedBlock != hitInfo.collider.gameObject)
                    {
                        if (selectedBlock != null)
                        {
                            selectedBlock.renderer.material.color = origColour;
                        }

                        origColour = hitInfo.transform.renderer.material.color;
                        hitInfo.transform.renderer.material.color = highlight;
                       
                        selectedBlock = hitInfo.collider.gameObject;

                        Camera.main.GetComponent<MouseOrbit>().target = selectedBlock.transform;
                        GameObject.Find("Manager").GetComponent<Manager>().hasClicked = true;
                   
                    }
                }
            }
        }


    }

bump can anyone help me convert the script above, used for changing the colour of a block when clicked, to changing the colour only when the mouse hovers over it and then changes back when the mouse isnt on it

Most attempts you had done where OK, you just needed to place them on the cube instead of the camera.

The camera script you showed sends a raycast towards the cube. The other one uses built in unity hovering detection that must be applied on the target object itself.

It’s like if you have one sniper script that shots targets and a foursquare app where the target is supposed to locate himself, except that you gave the foursquare app to the snipper instead of the target…

Thank you for that! ^
very insightful, iv now made a new little script, attached it to the prefab and it works great

using UnityEngine;
using System.Collections;

public class Highlight : MonoBehaviour
{

    private Color origColour;
    public Color HoverOver;

    void Start (){

        origColour = renderer.material.color;
    }
    void OnMouseEnter()
    {

        renderer.material.color = HoverOver;
        gameObject.renderer.material.color = HoverOver;
        }
   

    void OnMouseExit()
    {

        renderer.material.color = origColour;
       
    }
}
1 Like