How to use Events in Grabbable component

In this tutorial, we will focus on how to call a function through an Event call, specifically using Hover Entered and Hover Exited.

Before we start, you should really check the events in the manual first.

  1. Create a script; we will name it TestHover:

../../../_images/new_script.png

This is the implementation:

public class TestHover : MonoBehaviour
{
    public void TestHoverEntered()
    {
        Debug.Log("Interactor is hovering on interactable!");
    }

    public void TestHoverExited()
    {
        Debug.Log("Interactor stopped hovering on interactable!");
    }
}

The TestHoverEntered function will test the Hover Entered event works by printing a message to the console when its triggered.

The TestHoverExited function will test the Hover Exited event works by printing a message to the console when its triggered.

  1. Create an empty game object and attach the TestHover script to it.

    ../../../_images/new_object.png
  2. Open the Events of the Grabbable component and add a new element to the Hover Entered event.

    ../../../_images/new_element_hover.png

    In that new Element, make sure it’s Runtime only or Editor and Runtime.

    ../../../_images/runtime.png
  3. Drag and drop the new empty game object you made in Step 2.

    ../../../_images/drag_and_drop.png
  4. Click on “no function,” go to TestHover, and click TestHoverEntered, and you are done.

    ../../../_images/add_function.png
  5. Repeat steps 3-5 for Hover Exited.

Now, if you run the simulation and hover the interactor (hand) over the object, the message “Interactor is hovering on interactable!” will be printed on the console.

If you stop hovering over the object, the message “Interactor stopped hovering on interactable!” will be printed.

The same can be applied to the rest of the events like Select and Activate.