Optimization principal 2: Minimize the number of objects in a scene. As soon as an object is no longer needed, we should destroy it. This saves both memory and also processing resources, since Unity no longer needs to send the object Update()
and OnGUI()
messages, or consider collisions or physics for such objects, and so on.
However, there may be times when we wish not to destroy an object immediately, but at some known future point in time. Examples might include, after a sound has finished playing (see Chapter 6, Playing and Manipulating Sounds, for such a recipe), or perhaps the player only has a certain time to collect a bonus object before it disappears, or perhaps an object displaying a message to the player should disappear after a certain time (See Chapter 4, Creating GUIs, for such a recipe).
This recipe demonstrates how objects can be told to "start dying", and then to automatically destroy themselves after a given delay has passed.
To destroy objects after a specified time, perform the following steps:
Cube
.DeathTimeExample.cs
to Cube
:// file: DeathTimeExample.cs using UnityEngine; using System.Collections; public class DeathTimeExample : MonoBehaviour { public float deathDelay = 4f; private float deathTime = -1; private bool menuMode = true; private void OnGUI(){ if( menuMode ) GUIMenu(); else GUITimeDisplay(); } private void GUITimeDisplay(){ float timeLeft = Time.time - deathTime; GUILayout.Label("time left before death = " + timeLeft); } private void GUIMenu(){ bool startDyingButtonClicked = GUILayout.Button("Start dying"); if( startDyingButtonClicked ){ StartDying(); menuMode = false; } } private void Update() { if( deathTime > 0 && Time.time > deathTime ) Destroy( gameObject ); } private void StartDying() { deathTime = Time.time + deathDelay; } }
The OnGUI()
method
initially presents a button to the user, which when clicked causes the StartDying()
method to be called. After the button is clicked, the time left before the object destroys itself is displayed. Which of the two GUI methods is executed depends on the value of the Boolean variable menuMode
.
The float variable deathDelay
stores the number of seconds the object waits before destroying itself, once the decision has been made for the object to start dying. The float variable deathTime
either has a value of -1
(no death time set yet), or it is a positive value, which is the time we wish the object to destroy itself. When method StartDying()
is called, it sets this deathTime
variable to the current time plus whatever value is set in deathDelay
.
Every frame Update()
method calls the CheckDeath()
method. CheckDeath()
tests if deathTime
greater than zero (that is, a death time has been set), and then tests whether the current time has passed the death time. If the death time has passed, then the parent gameObject
is immediately destroyed.
When you run the scene, you'll see the Cube GameObject removed from the Hierarchy panel once its death time has been reached.
18.117.73.127