Select your preferred scripting language. All code snippets will be displayed in this language.
Namespace: UnityEngine
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseThe 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.
// Mark the PlayerScript as requiring a rigidbody in the game object. @script RequireComponent(Rigidbody)function FixedUpdate() { rigidbody.AddForce(Vector3.up); }
C# Example:
[RequireComponent (typeof (Rigidbody))] public class PlayerScript : MonoBehaviour { void FixedUpdate() { rigidbody.AddForce(Vector3.up); } }
RequireComponent | Require a single component. |