0

Here I have a little code and I am trying to drag object with OnMouseDown() function.I try bellow code but is not working. I have 2D object with 2Dcircle collider. This 2D circle collider can't detect OnMouseDown(). I want to detect mouse click, if object clicked not on other screen/ camera screen.

Here is my little code.

public  void OnMouseDown()
    {
        spring.enabled = false;
        clickedOn = true;
        Debug.Log (this.gameObject.name + " get down.");
    }

    public  void OnMouseUp()
    {
        spring.enabled = false;
        rgb2d.isKinematic = false;
        clickedOn = false;
        Debug.Log (this.gameObject.name + " get up.");
    }

This link also not working Link

public void OnPointerDown(PointerEventData eventData)
    {
        spring.enabled = false;
        clickedOn = true;
        Debug.Log (this.gameObject.name + " Was Clicked.");
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        spring.enabled = false;
        rgb2d.isKinematic = false;
        clickedOn = false;
        Debug.Log (this.gameObject.name + " Was Clicked.");
    }
Hellium
  • 7,206
  • 2
  • 19
  • 49
Mahi
  • 180
  • 3
  • 15
  • You should be using new Unity event system for this. `OnPointerDown` and `OnPointerUp` are the functions to use. For dragging, use `OnDrag`. See [here](http://stackoverflow.com/a/41392130/3785314) for more information. You use 2D so I think you should scroll down to *7.For 2D Object (Sprite Renderer/any 2D Collider).* – Programmer Apr 23 '17 at 11:57
  • @Programmer I allready try it but not working. – Mahi Apr 23 '17 at 12:02
  • I doesn't work like that. Update your question with code from the linked answer that doesn't work. You can't say "it doesn't work" without adding the code. Edit your question and that new code there not on the comment section. – Programmer Apr 23 '17 at 12:07
  • @Programmer it gives me error. **error CS0535: `Projectile_dragging' does not implement interface member `UnityEngine.EventSystems.IPointerClickHandler.OnPointerClick(UnityEngine.EventSystems.PointerEventData)'** – Mahi Apr 23 '17 at 12:07
  • You have to include the class name too. Just post the whole script. You are missing something. – Programmer Apr 23 '17 at 12:32
  • I already add class name like monobeahavior, ... – Mahi Apr 23 '17 at 12:36
  • I was talking about the updated code in your answer. Put your full script there so that somebody can help you. I will close this as a duplicate. You can either implement the missing function or copy the code as it is directly. You may want to read *7.For 2D Object (Sprite Renderer/any 2D Collider)* since it says you must add `Physics2DRaycaster` to the camera and provides a script to do that. Happy coding! – Programmer Apr 23 '17 at 12:41

0 Answers0