- Home /
Question by
sanks007 · Jul 02, 2013 at 02:52 PM ·
mouseraycastingclick
Click on GameObject leads to multiple call
Hey guys facing a strange issue.While detecting the click on gameobjects using Raycasting,sometimes the code gets executed multiple times. Here is the code
void Update ()
{
if (Input.GetMouseButton(0))
{
Ray ray;
RaycastHit raycast;
ray=Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out raycast))
{
switch( raycast.collider.name)
{
case "First_Sphere":
GameObject go = (GameObject)Instantiate(Resources.Load("1ValenceBondElementPrefab"),new Vector3(0,0,0),Quaternion.identity);
break;
}
}
}
}
What leads to this multiple calling and why sometimes it leads to multiple calls and sometimes it is executed properly
Comment
Best Answer
Answer by Owen-Reynolds · Jul 02, 2013 at 02:54 PM
Look up http://docs.unity3d.com/Documentation/ScriptReference/Input.html
GetMouseButton checks if it is clicked or held. GetMouseButtonDown checks it was first pressed this frame.