addcomponent unity code example
Example 1: unity requirecomponent
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerScript : MonoBehaviour
{
Rigidbody rb; void Start()
{
rb = GetComponent<Rigidbody>();
} void FixedUpdate()
{
rb.AddForce(Vector3.up);
}
}
Example 2: Add component object to gameobject unity
gameobject.AddComponent<Rigidbody>();
gameobject.AddComponent<MeshCollider>();
Example 3: unity add component
GameObject gameObject;
gameObject.AddComponent<BoxCollider>();
Example 4: c# addcomponent
Rigidbody addedComp = gameObj.AddComponent<Rigidbody>() as Rigidbody;
Example 5: On add component unity
void Reset()
{
}
Example 6: how to add a componet to a gameobject throgh code unity
gameobject.addComponet<ridgidbody>();
gameobject.addComponet<MeshCollider>();