Relevant VE2 documentation is linked at the bottom of this page
<aside> 🎓 If you’re attending this course through the ECRI, this page contains the contents of the first in-person lesson. It could act as a useful reference for you after the session, but please don’t go through the material before the session or you’ll be bored!
</aside>
Last lesson, you learned how to create and configure an activatable button and link it up to toggle the active state of gameobjects in your scene. This gave you a flavour of the VE2 components and allowed you to achieve some basic interactions without any code.
If you want your activatable to do anything more substantive in the scene, you need to link it up to your own code. Luckily, this is very straightforward!
First, create public methods to be called upon activation and deactivation of your activatable. Make sure these methods are in a script attached to some gameobject in your scene, then drag and drop that gameobject into the activatable’s OnActivate and OnDeactivate fields. Now you can select the method you wish to link for each of those events.
The activatable will call your script’s functions automatically when activated and deactivated. Remember, when linking a script to an activatable, it is crucial to drag and drop the gameobject in the scene containing that script into the OnActivate/OnDeactivate fields, rather than dragging and dropping the script directly from the project view.


<aside> 📝 Exercise One - Controlling the target
So far, to move our target, we’ve been linking our buttons to “OnActivate” and “OnDeactivate” methods in our code, and using those methods to set the value of a boolean, which we then check within our Update loop.
This works, but it’s a bit verbose. Wouldn’t it be easier if we could just ask the button directly from our Update loop if its currently pressed, and do away with all our boilerplate “OnActivate” and “OnDeactivate” methods? Luckily, we can!
Referencing VE2 components has a slightly different syntax to regular Unity components. Rather than declaring a field of the corresponding type, instead, you must declare a field of type InterfaceReference. This is a generic class which takes a type parameter, which should be the interface for the VE2 component you want to talk to, i.e, the name of the VE2 component, prefixed with the letter I. The interface itself is then accessed with fieldname.Interface, as in the example below

<aside> ℹ️
When to use which approach
Now you have two two different method to linking your scripts to VE2 components, the appropriate approach depends on the use case
InterfaceReference fields and assigning them to the VE2 components in your script’s inspector and talking to the interface programmatically. This is useful when we want to poll the state of the component each frame, or on frames where the event may not have been emitted.
</aside><aside> 📝 Exercise Two - Cleaning up our code
In order to create the ping pong gun, you must first learn how to make an object grabbable. This can be achieved by attaching a V_FreeGrabbable component to any gameobject with a Rigidbody (if a Rigidbody isn’t already attached, one will appear automatically). This will allow players to “grab” that gameobject and carry it around. Like activatables and adjustables, V_FreeGrabbables expose various configurations in the inspector, such as events that are emitted when the grabbable is grabbed or dropped.