Destroy after: Place this script in an Unity 3D game object that you want/need to destroy after a set period of time. There is a variable timer which enables you to enter the time via the inspector. This also allows you to use the same script on multiple objects.
#pragma strict
public var timerDestroy : int;
function Start () {
yield WaitForSeconds(timerDestroy);
Destroy (gameObject);
}
Destroy on Enter: This script is activated when an object enters the trigger collider of the object containing this script. This code has the variable 'tagName' so you may reuse the script on various objects and enter unlimited tags.
#pragma strict
public var tagName: String = "Tag";
function OnTriggerEnter (Col : Collider)
{
if(Col.tag == tagName)
{
Destroy (Col.gameObject);
}
}
Destroy on Click: This script does exactly that: Click on any object containing this script and it is immediately destroyed.
#pragma strict
private var mouseOver = false;
function OnMouseOver()
{
mouseOver = true;
}
function OnMouseExit()
{
mouseOver = false;
}
function OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
yield WaitForSeconds(0.3);
Destroy(gameObject);
}
}
Destroy and replace: This script functions exactly like the previous one; however, after the object is destroyed it is replaced by the object of your choice. You can instantiate any object as a replacement which includes ragdolls and destructible objects constructed in Blender.
#pragma strict
var destructible : Transform;
var parent : Transform;
private var mouseOver = false;
function OnMouseOver()
{
mouseOver = true;
}
function OnMouseExit()
{
mouseOver = false;
}
function OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
yield WaitForSeconds(0.3);
Instantiate (destructible, parent.transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Destroy with Alert: This script is very similar to the Destroy on Click script except here a pop-up gives you the option of destroying the object or exiting the window. There is also an optional second window that double-checks if they reallllyyyy want to destroy the object
#pragma strict
var destructible : Transform;
var parent : Transform;
private var mouseOver = false;
var alertskin : GUISkin;
var optionsGUI = false;
var okaytodestroyGUI = false;
var optionsopenGUI = false;
function Start(){
optionsGUI = false;
okaytodestroyGUI = false;
}
function OnMouseOver()
{
mouseOver = true;
}
function OnMouseExit()
{
mouseOver = false;
}
function OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
optionsGUI = true;
}
}
function OnGUI()
{
if (optionsGUI)
{ optionsopenGUI = true;
if (optionsopenGUI)
OptionsGUI ();
}
if (okaytodestroyGUI)
{DestroyAlertGUI (); }
}
function OptionsGUI()
{
GUI.skin = alertskin;
var style1 = alertskin.customStyles[0];
Screen.lockCursor = false;
GUI.TextArea (Rect (240, Screen.height - 450,272,62), "Destroy this object?");
if (GUI.Button (Rect(378, Screen.height - 389,140,35),"Yes")){
okaytodestroyGUI = true;
optionsGUI = false;
}
if (GUI.Button (Rect(233, Screen.height - 389,140,35),"No"))
{
optionsGUI = false;
}
}
function DestroyAlertGUI () {
GUI.skin = alertskin;
var style1 = alertskin.customStyles[0];
Screen.lockCursor = false;
GUI.TextArea (Rect (240, Screen.height - 450,272,62), "Removing object now. Sound good?");
if (GUI.Button (Rect(378, Screen.height - 389,140,35),"Yes")){
Instantiate (destructible, parent.transform.position, Quaternion.identity);
Destroy (gameObject);}
if (GUI.Button (Rect(233, Screen.height - 389,140,35),"No")) {
okaytodestroyGUI = false;
}
}