Manual     Reference     Scripting  
  
Scripting > Attributes > RequireComponent   
  • Menu
  • Overview
  • Runtime Classes
  • Attributes
  • Enumerations
  • Editor Classes
  • Enumerations
  • History
  • Index
  • Attributes
  • AddComponentMenu
  • ContextMenu
  • ExecuteInEditMode
  • HideInInspector
  • NonSerialized
  • RPC
  • RenderBeforeQueues
  • RequireComponent
  • Serializable
  • SerializeField

RequireComponent Class, inherits from Attribute  

The RequireComponent attribute lets automatically add required component as a dependency.

When you add a script which uses RequireComponent, the required component will automatically be added to the game object. This is useful to avoid setup errors. For example a script might require that a rigid body is always added to the same game object. Using RequireComponent this will be done automatically, thus you can never get the setup wrong.

// C# example:
// Mark the PlayerScript as requiring a rigidbody in the game object.
[RequireComponent (typeof (Rigidbody))]
class PlayerScript : MonoBehaviour
{
void FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
}

Constructors
RequireComponent

Require a single component