OnMouseDown don't work

I use UI Text and add component “BoxCollaider2D”(“BoxCollaider”), but the button is not clickable. Why? Is Trigger set to true.

    void OnMouseDown() {
        SceneManager.LoadScene("scene2");
    }

@KoVicH

I don’t see any problem with the OnMouseDown Method part, maybe it’s something else?

Oops. I’ll take that back.

You are using new UI, don’t add colliders to buttons, just create script that implements one of the click event methods:

using UnityEngine;
using UnityEngine.EventSystems;

public class ClickToLoad: MonoBehaviour, IPointerClickHandler
{
  public void OnPointerClick(PointerEventData eventData)
  {
    SceneManager.LoadScene("scene2");
  }
}

EDIT:
but if you want to click a gameobject instead of button / other UI canvas object:

  1. You’ll have to add box collider (or other collider) to your object - no need to make it trigger
  2. Add Physic Raycaster to your scene camera
  3. Also have Eventsystem in scene
  4. Add your script with OnMouseDown to clickable gameobject… but then again, OnPointerClick works as well.

OnMouseDown works like this:
https://unity3d.com/learn/tutorials/modules/beginner/scripting/on-mouse-down

It’s meant for old GUI and Colliders.

1 Like

Thanks:)