Game Development Reference
In-Depth Information
As is, this script would work in this situation. However, currently it always
destroys the trigger and sound after the sound is played. We need to add
further flexibility to this script so we can choose whether to destroy the
trigger or not.
Step 25 : Declare a Boolean variable:
var door : GameObject;
var doorOpenSound : GameObject;
var destroyWhenFinished : boolean;
function OnTriggerEnter (other:Collider){
iTween.RotateTo(door, Vector3(0,-120,0), 5.0);
doorOpenSound.audio.Play();
yield WaitForSeconds (doorOpenSound.audio.clip
.length);
Destroy (doorOpenSound);
Destroy (gameObject);
}
Why?
By making this Boolean a public variable, we will make it a check box that
is accessible in the Inspector later.
Step 26: Make the script check to see if the Boolean destroyWhenFinished
is true before destroying:
var door : GameObject;
var doorOpenSound : GameObject;
var destroyWhenFinished : boolean;
function OnTriggerEnter (other:Collider){
iTween.RotateTo(door, Vector3(0,-120,0), 5.0);
doorOpenSound.audio.Play();
yield WaitForSeconds (doorOpenSound.audio.clip
.length);
if (destroyWhenFinished){
Destroy (doorOpenSound);
Destroy (gameObject);
}
}
Why?
Take a close look at that—it really adds one line of code (if (destroyWhen
Finished)), but be careful to notice that it puts the destroys inside {} .
What's happening there is if the Boolean state of destroyWhenFinished is
true it executes the block of code that does the destroys.
Search WWH ::




Custom Search