举例添加脚本组件
...
void Start () {
gameObject.AddComponent("script_new");//添加脚本组件
Invoke("delete11", 1);
}
void delete11()
{
Destroy(gameObject.GetComponent("script_new"), 1); // 销毁脚本组件
}
script_new.cs
..
using UnityEngine;
using System.Collections;
public class script_new : MonoBehaviour {
// Use this for initialization
void Start () {
print("new");
}
// Update is called once per frame
void Update () {
}
void OnDestroy()
{
print("destory");
}
}