- Home /
Enforce component attachment
My search skills must be failing me, or maybe I am asking the wrong question. I have a way in c# to programmatic enforce attached components. For example if I attach a specific script the script will cause an error if a rigid-body is not contented. But I can not seem to remember the syntax.
Comment
Best Answer
Answer by by0log1c · Apr 12, 2011 at 12:33 PM
I think this is what you want. Note the lack of ; .
@script RequireComponent(Rigidbody)
C#
[RequireComponent (typeof (Rigidbody))]
class PlayerScript : MonoBehaviour {
void FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
}
Examples from Unity Script Reference.
There it is
[RequireComponent (typeof (Component))]
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613122242im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Change color of all buttons listed in array 3 Answers
Storing component of instance in an array 2 Answers