void DestroyGameObject()
{
Destroy(gameObject);
}
void DestroyScriptInstance()
{
// Removes this script instance from the game object
Destroy(this);
}
void DestroyComponent()
{
// Removes the rigidbody from the game object
Destroy(GetComponent<Rigidbody>());
}
oid DestroyObjectDelayed()
{
// Kills the game object in 5 seconds after loading the object
Destroy(gameObject, 5);
}
// When the user presses Ctrl, it will remove the
// BoxCollider component from the game object
void Update()
{
if (Input.GetButton("Fire1") && GetComponent<BoxCollider>())
{
Destroy(GetComponent<BoxCollider>());
}
}
DontDestroyOnLoad
切换场景时,原场景的物体都会被删除,使用这个会保留原场景的物体
FindObjectOfType
// Search for any object of Type GUITexture, // if found print its name, else print a message // that says that it was not found. public class ExampleClass : MonoBehaviour { void Start() { GUITexture texture = (GUITexture)FindObjectOfType(typeof(GUITexture)); if (texture) Debug.Log("GUITexture object found: " + texture.name); else Debug.Log("No GUITexture object could be found"); } }
检索所有物体的组件的名称,将符合条件的选出来。若检索到第一个符合条件的,将不会继续检索后面的
FindObjectsOfType
检索所有符合条件的组件,返回一个数组
