0

So I'm trying to do this thing where if I left click on an object, 1 is added to a variable, and if I right click on it, 1 is subtracted from that variable. The left click works fine, but when I right click, nothing is happening.

 public class cs_SliderClick : MonoBehaviour 
 {

     public int sliderValue;

     void Start () 
     {

     }

     void Update () 
     {

     }

     public void OnMouseDown()
     {
         if (Input.GetMouseButtonDown(0))
         {
             sliderValue += 1;
         }

         if (Input.GetMouseButtonDown(1))
         {
             sliderValue -= 1;
         }
     }
 }

Can anyone tell me what I'm doing wrong here?

Thanks.

  • 1
    you need to check on the MouseButtonUp event have you done a simple google search on `C# how to check for Right Mouse Button Click` http://stackoverflow.com/questions/19448346/how-to-get-a-right-click-mouse-event-changing-eventargs-to-mouseeventargs-cause – MethodMan Jul 06 '16 at 21:24
  • 1
    @MethodMan This is for Unity not windows form – Programmer Jul 06 '16 at 21:26
  • 1
    WIndows has a MouseUp Event too.. do a google search come on now.. – MethodMan Jul 06 '16 at 21:27
  • @MethodMan Dude this is Unity3D. The link you posted is for Windows Form. If he mange to import the dlls, that's it. This wont work on Lniux, Mac Os, Android, iOS and other platforms. He must use Unity's build in API to read the from Input devices. I suggest you Google `Unity3D`. – Programmer Jul 06 '16 at 21:47
  • I would suggest the OP google Unity3D Mouse Click Events – MethodMan Jul 06 '16 at 21:48

2 Answers2

3

You need to use Unity's EventSystems.

Implement IPointerClickHandler and then override the OnPointerClick function.

Attach PhysicsRaycaster to the camera if the GameObject is a 3D Mesh. If this is a 2D game then attach Physics2DRaycaster to the camera.

Below is your fixed code:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class cs_SliderClick : MonoBehaviour, IPointerClickHandler
{
    public int sliderValue;

    void Start()
    {
        //Attach PhysicsRaycaster to the Camera. Replace this with Physics2DRaycaster if the GameObject is a 2D Object/sprite
        Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
        addEventSystem();
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            Debug.Log("Left click");
            sliderValue += 1;
        }

        else if (eventData.button == PointerEventData.InputButton.Right)
        {
            Debug.Log("Right click");
            sliderValue -= 1;
        }
    }


    //Add Event System to the Camera
    void addEventSystem()
    {
        GameObject eventSystem = null;
        GameObject tempObj = GameObject.Find("EventSystem");
        if (tempObj == null)
        {
            eventSystem = new GameObject("EventSystem");
            eventSystem.AddComponent<EventSystem>();
            eventSystem.AddComponent<StandaloneInputModule>();
        }
        else
        {
            if ((tempObj.GetComponent<EventSystem>()) == null)
            {
                tempObj.AddComponent<EventSystem>();
            }

            if ((tempObj.GetComponent<StandaloneInputModule>()) == null)
            {
                tempObj.AddComponent<StandaloneInputModule>();
            }
        }
    }

}
Programmer
  • 121,791
  • 22
  • 236
  • 328
0

So I would recommend to call the OnMouseDown() Function inside of the Update Method.

public class cs_SliderClick : MonoBehaviour {

     public int sliderValue;

     void Start () 
     {

     }

     void Update () 
     {
         OnMouseDown();
     }

     public void OnMouseDown()
     {
         if (Input.GetMouseButtonDown(0))
         {
             sliderValue += 1;
         }

         if (Input.GetMouseButtonDown(1))
         {
             sliderValue -= 1;
         }
     }
 }
  • If you click `edited` on my answer, you will notice that this was my first answer. I removed it after I realized that he doesn't want to detect a click. Hes wants wants to detect click **on an object**. This code will work whether the Object is clicked or not. – Programmer Jul 06 '16 at 22:01
  • I think this solution works best for me. The one thing that's strange, however, is that when I left click, the function gets called twice, but when I right click, it only gets called once. I will investigate this further, but if anyone knows why that is happening, I'd be interested to know. Thanks. – MerlinsMaster Jul 07 '16 at 00:56
  • I think the problem with the double left click has nothing to do with this code. Maybe try to test this code in a new project to see if it working correctly. If it does there is maybe a second script somewhere that's killing you softly. – Rudi Ruessolds Jul 07 '16 at 06:27