Called on releasing the mouse button while the mouse pointer is over the Collider attached to this GameObject.
The key factor is where the mouse button is released rather than where it was initially pressed down:
OnMouseUp
is called on that collider.OnMouseUp
is not called on that collider.For button-like behavior (requiring both press and release over the same collider), use MonoBehaviour.OnMouseUpAsButton.
This event is sent to all scripts attached to the Collider. This function is not called on objects that belong to Ignore Raycast layer. This function is called on Colliders and 2D Colliders marked as trigger when the following properties are set to true:
OnMouseUp can be a coroutine.
Additional resources: OnMouseDown, OnMouseDrag.
Note: New projects created with this version of Unity are pre-configured to use a version of the Input System that doesn't support this callback. To support this callback in your project, you can change the Active Input Handling setting in Player settings to either Both or Input Manager (Old). However, doing so is not recommended as the legacy Input Manager is nearing the end of support and Input System is the recommended solution for new projects. For more information, refer to Migrating from the old Input Manager.
// Register when mouse dragging has ended. OnMouseUp is called // when the mouse button is released.
using UnityEngine;
public class ExampleClass : MonoBehaviour { void OnMouseUp() { // If the user releases the mouse button while over the GameObject with this script attached, output this message Debug.Log("Drag ended!"); } }