Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


RequireComponent

The RequireComponent attribute automatically adds required components as dependencies.
Read time 4 minutesLast updated 6 days ago

Definition

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]public sealed class RequireComponent : Attribute

Remarks

When you add a script which uses RequireComponent to a GameObject, the required component is automatically added to the GameObject. This is useful to avoid setup errors. For example a script might require that a Rigidbody is always added to the same GameObject. When you use RequireComponent, this is done automatically, so you are unlikely to get the setup wrong.
Note: RequireComponent only checks for missing dependencies when GameObject.AddComponent is called. This happens both in the Editor, or at runtime. Unity does not automatically add any missing dependences to the components with GameObjects that lack the new dependencies.

Examples

using UnityEngine;// PlayerScript requires the GameObject to have a Rigidbody component[RequireComponent(typeof(Rigidbody))]public class PlayerScript : MonoBehaviour{ Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void FixedUpdate() { rb.AddForce(Vector3.up); }}

Constructors

Constructor

Description

RequireComponent(System.Type)Require a single component.
RequireComponent(System.Type,System.Type)Require two components.
RequireComponent(System.Type,System.Type,System.Type)Require three components.

Inheritance

Inherited Members