Autofill is a simple Unity extension that can automatically assign serialized component references for you at edit time. If a reference is successfully autofilled, it will be hidden in the Inspector, allowing you to safely ignore it like the implementation detail it is. This provides all the ease of use of calling GetComponent with none of the runtime costs!
Autofill turns messy Inspectors like this:
into this:
We recommend you install Autofill via OpenUPM. Per OpenUPM's documentation:
- Open
Edit/Project Settings/Package Manager - Add a new Scoped Registry (or edit the existing OpenUPM entry) to read:
- Name:
package.openupm.com - URL:
https://package.openupm.com - Scope(s):
com.jonagill.autofill
- Name:
- Click Save (or Apply)
- Open Window/Package Manager
- Click the + button
- Select Add package by name...
- Click Add
To enable Autofill for a serialized field on a given component, you must mark it up with the [Autofill] or [AutofillOptional] attributes.
A serialized component field marked with [Autofill] will be automatically filled whenever the component is viewed in the Inspector or a prefab containing the component gets saved. If the component is found, we hide the field in the Inspector to avoid cluttering the component with useless property drawers.
If the component is not found, we display an error into the Inspector like so:
A serialized component field marked with [AutofillOptional] will be automatically filled whenever the component is viewed in the Inspector or a prefab containing the component gets saved. If the component is found, we hide the field in the Inspector to avoid cluttering the component with useless property drawers.
Unlike [Autofill], we do not display an error if no component could be found, as this component is understood to be optional. Any references to that component should be wrapped in nullity checks within your component logic.
Both [Autofill] and [AutofillOptional] take a number of constructor parameters:
The type parameter specifies where the Autofill system will search to try and find a valid target component to assign to the autofilled reference. There five possible options:
- Self (default)
- Autofill will only check the GameObject containing the declaring component for the target component.
- This is equivalent to a
GetComponentcall.
- Parent
- Autofill will check all the parents of the GameObject containing the declaring component for the target component.
- SelfAndParent
- Autofill will check both the GameObject containing the declaring component and all of its parents for the target component.
- This is equivalent to a
GetComponentInParentcall (since that call also checks the invoking object for the desired component).
- Children
- Autofill will check all descendants of the GameObject containing the declaring component for the target component.
- SelfAndChildren
- Autofill will check both the GameObject containing the declaring component and all of its descendants for the target component.
- This is equivalent to a
GetComponentInChildrencall (since that call also checks the invoking object for the desired component).
The Autofill system usually displays an error if it finds multiple valid target components when trying to fill a reference. If this parameter is set to true, the Autofill system will accept the first result it finds (usually the closest component to the declaring component in the hierarchy).
The Autofill will usually hide autofilled references that have been successfully filled in the Inspector. If this parameter is set to true, the Autofill system will draw a disabled version of this field instead of hiding it entirely.
Whenever you save or import a prefab containing an [Autofill] reference that could not be resolved, an error dialog will display:
This dialog gives you three options:
- Okay: Simply dismisses the dialog, allowing you to go and fix this single issue.
- Disable all warnings until restart: Dismisses the dialog and prevents additional dialogs from appearing until you restart Unity. Can be useful if you are bulk-importing a lot of assets with errors and receiving a lot of dialogs at the same time.
- Don't warn again for this prefab: Marks this error as expected and prevents additional dialogs for opening for this exact property on this exact prefab. This is tracked by adding a line to a
AutofillIgnoredErrors.txtfile in yourProjectSettingsfolder. This file can be committed into version control, which makes this option helpful for e.g. marking a missing Autofill reference as expected on a base prefab that will usually be dropped into a context where the reference can be resolved.
Of course, if you expect that this reference will be missing on a lot of assets, can also use [AutofillOptional] to mark the missing reference as intentional, which will suppress the error dialog entirely.
- Autofill is not currently supported on serialized arrays or lists.



