Problem With OnMouseX

Hi,

I try to use mouse to detect clicks on gameobject. I create simple cube 1 x 1 x 1 on the cenetr of terrain and then I put code for clicks like below. This is javascript and I get only one time each clicks(Debug messages) and I think this should work every time. And I also check the box Collider is on and there are no other gameobject and I use default layer on the scene

function OnMouseEnter() {

	Debug.Log("Enter");

}

function OnMouseExit() {
	Debug.Log("Exit");
 
}

Why I get only one Debug-message from each OnMouseX-functions???

I am using Win 7 Ultimate and Unity 3.4.1f5

  • Please Help Me!

I agree with you, syclamoth. Also, I would avoid using OnMouseEnter, for my own superstitious reasons. I haven’t had good luck with that function, for some reason.

Secondly, you’ll need to use some raycasting here, I think. Here’s some code of mine that detects an object with a tag that has been clicked on. Just put it in your code and change the tag, and you’ll be set to go.

Cheers!

function Update ()
{
   if (Input.GetMouseButtonDown(0))
   {
      var hit : RaycastHit;
      var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
      if (Physics.Raycast (ray, hit, 100.0))
      {
          myTarget = hit.collider.gameObject;
          if(myTarget.tag == "ENTER YOUR TAG HERE")
          {
      	  	//DO WHATEVER
          }
      }
   }
}