O
O
OkeanKoda2021-12-04 20:58:28
Unity
OkeanKoda, 2021-12-04 20:58:28

Why doesn't OnMouseDown() work?

Tried OnPointerClick, doesn't work either. I've been fighting for about two hours already ( It
doesn't cross other colliders. I
threw the child objects into the IgnoreRaycast layer and shifted along the z axis, it didn't help either.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class OnClickSquare : MonoBehaviour
{
    public void OnMouseDown()
  {
    Debug.Log("клик");
  }
}

61ababec2147d773805224.png

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
GFX Data, 2021-12-05
@OkeanKoda

Here is an example without a button, a UI element works (green) and a simple object with BoxCollider2D (red)
- an object with an EventSystem is needed in the scene
- for colliders on the camera there must be a Physics2DRaycater
- for UI elements the canvas must have a GraphicRaycaster
- the listening object must have a script with implementation IPointerDownHandler

using UnityEngine;
using UnityEngine.EventSystems;

public class PointerListener : MonoBehaviour, IPointerDownHandler
{
    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("## " +name + " - "+ eventData.ToString());       
    }
}

DOWNLOAD PROJECT [2022.1.0a16]
61ac9d77c6c52796402930.jpeg

F
freeExec, 2021-12-04
@freeExec

There should also be an EventSystem on the stage. If you select it, it will show what kind of objects are under the mouse and who is in the way. Actually, no colliders are needed.

M
mustGamedev, 2021-12-05
@mustGamedev

IPointerDown try

O
OkeanKoda, 2021-12-05
@OkeanKoda

As a result, I just hung a button on the object, an Image with RaycastTarget and a script

public void OnClick()
  {
    if (isRight == true)
    {
      //анимация частиц
    }
    else
    {
      Debug.Log("клик");
    }
That's how it works. But how to make a click without a button did not understand)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question