AddComponent
Adds a component of the specified type to the GameObject.
Read time 1 minuteLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
AddComponent(Type)
Adds a component of the specified type to the GameObject.
public Component AddComponent(Type componentType)
Parameters
Returns
Type | Description |
|---|---|
| Component |
Remarks
There is no corresponding method for removing a component from a GameObject. To remove a component, use Object.Destroy.
using UnityEngine;using System.Collections;public class AddComponentExample : MonoBehaviour{ void Start() { SphereCollider sc = gameObject.AddComponent(typeof(SphereCollider)) as SphereCollider; }}
Additional Resources: Component, Object.Destroy
AddComponent<T>()
Generic version of this method.
public T AddComponent<T>() where T : Component
Returns
Type | Description |
|---|---|
| T |
Remarks
using UnityEngine;using System.Collections;public class AddComponentExample : MonoBehaviour{ void Start() { SphereCollider sc = gameObject.AddComponent<SphereCollider>(); }}
Additional Resources: Component, Object.Destroy