Answer the question
In order to leave comments, you need to log in
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("клик");
}
}
Answer the question
In order to leave comments, you need to log in
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());
}
}
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.
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 questionAsk a Question
731 491 924 answers to any question