How to make object detect mouse is over it.

How would I make so a Prefab would detect that my mouse is over it, something like

private var MouseOver : boolean = false;

if(MouseOver == true) {
}

That’s just something I whipped up, but that’s kind of the format I am looking for, Please help, and thanks in advance!

Two options:

  1. Using OnMouseOver callback
// assuming that your object has collider and script is attached to that object
void OnMouseOver() {
        Debug.Log("mouse is over object");
    }
  1. Using Raycast (which is more generic way) - Unity - Scripting API: Physics.Raycast

Any way you could give me a script that is in JavaScript? Thanks in advance!

Documentation contains JavaScript examples. For first solution, change “void” to “function”.

The only problem is that I have it detecting if player is within range, and that the mouse is over so I need it to be an If not function, any way to do that?

Put inside OnMouseOver function:

var distance : Vector3 = Vector3.Distance(this.transform.position,playerGO.transform.position);

if(distance<=requiredDistance)
{
    Debug.Log("player with in range and mouse is over the object");
}

Here is how my script works: This is the whole thing!

it interacts with other scripts but this is the pickup / range script I use, how would I put that stuff above into this?

#pragma strict


var ButtonToPress : KeyCode = KeyCode.E; //The button to press when picking up the item.
var PickUpDistance = 1.7f; //The distance from where the Item can be picked up. Remember that this is relative to the center of the Item and the center of the Player.

//These store information about the Item, if we can pick it up, the Player and the distance to the Player.
private var canPickUp = false;
private var theItem : Item;
private var thePlayer : Transform;
private var dist = 9999f;

@script AddComponentMenu ("Inventory/Items/First Person Pick Up")
@script RequireComponent(Item)

//This is where we find the usefull information which we can later access.
function Awake ()
{
	theItem = (GetComponent(Item));
}

function RetrievePlayer (theInv : Inventory)
{
	thePlayer = theInv.transform.parent;
}

function OnGUI ()
{
	
	if (canPickUp == true)
	{
		if (transform.name.Length <= 7)
		{
			 
		}
		else
		{
			
		}
	}
}

function Update ()
{
	if (thePlayer != null)
	{
		//This is where we enable and disable the Players ability to pick up the item based on the distance to the player.
		dist = Vector3.Distance(thePlayer.position, transform.position);
		if (dist <= PickUpDistance)
		{
				canPickUp = true;
		}
		else
		{
			canPickUp = false;
		}
		
		//This is where we allow the player to press the ButtonToPress to pick up the item.
		if (Input.GetKeyDown(ButtonToPress)  canPickUp == true)
		{
			theItem.PickUpItem();
		}
	}
}

//This is just for drawing the sphere in the scene view for easy testing.
function OnDrawGizmosSelected () 
{
	Gizmos.color = Color.yellow;
	Gizmos.DrawWireSphere (transform.position, PickUpDistance);
}

I tried this but it didn’t work.

function OnMouseOver () {
	if (thePlayer != null)
	{
		//This is where we enable and disable the Players ability to pick up the item based on the distance to the player.
		dist = Vector3.Distance(thePlayer.position, transform.position);
		if (dist <= PickUpDistance)
		{
				canPickUp = true;
		}
		else
		{
			canPickUp = false;
		}
		
		//This is where we allow the player to press the ButtonToPress to pick up the item.
		if (Input.GetKeyDown(ButtonToPress)  canPickUp == true)
		{
			theItem.PickUpItem();
		}
	}
}

The Problem: When I hit E nothing happened. My mouse was over the item!

Sorry if this is bothering you, I just really need to get this done soon as I have little time to get this done.

I’m sure that “Input.GetKeyDown(ButtonToPress)” will work in a OnMouseOver callback. It needs to be somewhere in Update().

Can you give me an example based on the code I gave you? My mind is really not awake right now its been a long day. Sorry!