Components#
Unity's GetComponent searches the object on every call. Visual Script's extension methods cache successful lookups during Play mode, which avoids repeated searches in Signals and other hot paths.
The methods live in VisualScript.Runtime and extend both GameObject and Component:
Rigidbody body = gameObject.Get<Rigidbody>();Get<T>() returns the cached component after the first successful lookup, or null when the object has no matching component.
The methods#
Choose a method based on whether a missing component should return a result or be added:
| Method | Does | Returns |
|---|---|---|
Get<T>() |
Fetches a component. | The component, or null. |
TryGet<T>(out T) |
Fetches a component. | true if it exists, with the component in the out parameter. |
Has<T>() |
Tests for a component. | true if it exists. |
Add<T>() |
Adds a component. | The new component. |
Require<T>() |
Fetches a component, adding it if missing. | The component, always. |
Each has a System.Type overload, such as Get(type) or Require(type), for when the type isn't known at compile time.
Require<T>() returns an existing component or adds one when missing. Without it, the caller performs both operations explicitly:
Rigidbody body = target.GetComponent<Rigidbody>();
if (body == null) body = target.AddComponent<Rigidbody>();Require<T>() combines the same contract into one call:
Rigidbody body = target.Require<Rigidbody>();Visual Script uses Require<T>() to attach runtime support on demand. For example, uGUI Triggers add a handler when an Event first observes a button.
How the caching works#
The first call on a game object attaches a hidden GameObjectReference component that maps types to cached components. After a successful lookup, later calls for that type read from the dictionary instead of searching the object again.
The component is registered with the auxiliary hide flags, so it doesn't appear in the Inspector unless you turn off Hide Auxiliary Components in the Editor settings.
Outside Play mode nothing is cached
Outside Play mode, Visual Script skips cache registration and each method uses GetComponent or AddComponent directly. The returned results stay the same, but lookups aren't reused.
Two cache rules affect repeated lookups:
- Destroyed entries are refreshed. A later call detects the missing component and searches again, so re-adding one replaces the stale reference.
- Misses are not cached.
Has<T>()on an object withoutTrunsGetComponentevery time, so avoid that pattern inside an update loop.
Where to go next#
- Events and Runners — run designer-authored logic from a component.
- Args — use cached source and target component accessors.
- Conventions — apply these methods inside a custom node.