- Home /
playing an audio clip before destroying object
Hi, I have the following script assigned to the key object. I want it to play an audio sound before I destroy the object, but it doesn't play the audio.
Do I have to delay destroying the object until the audio has completed? I hope not as it means the key will exist in the scene before disappearing, unless of course I just disable the game objects mesh until the audio finishes?
I am not really sure on the best way to do this, any ideas?
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class ScriptObjectKeyPickup : MonoBehaviour {
ScriptPlayerOneManager script;
// Use this for initialization
void Start () {
script = GameObject.Find("GameObjectPlayerOne").GetComponent<ScriptPlayerOneManager>();
}
// Update is called once per frame
void Update () {
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "GameObjectPlayerOne")
{
audio.Play();
script.playerInventory.playerOneKeyAmount += 1;
Destroy(gameObject);
}
}
}
Answer by Q-nn · Jul 13, 2014 at 01:25 PM
You've got the right idea, generally. Destroying an object playing a sound will stop the sound.
Some suggestions:
Destroy the components you don't want anymore immediately (renderer for example) and use the optional delay parameter on the Destroy call to destroy the gameObject itself after the audio has finished (you can get the length of the clip with audio.clip.length
, or just give it a few seconds if you like).
Have a separate object that plays the sound - you could enable it, call a script function that plays a sound or instantiate a whole SFX prefab if you wanted - though if you're instantiating it, you might as well use the built-in AudioSource.PlayOneShot().
Answer by instruct9r · Jul 13, 2014 at 01:23 PM
The sound is not played because you destroy the game object and there's nothing to play the sound.
Deactivate the mesh and the collider, play the sound and destroy the gameObject...
use:
yield WaitForSeconds (audio.clip.length)
To yield while the audio finishes and then destroy the object...
One more thing. $$anonymous$$ake sure that the first thing, that you do after the "if" statement is to deactivate the collider and then the mesh. This way if you go through the collider again, before the gameObject get's destroyed, you won't trigger it again, which might cause double collection of the object...
Answer by NerdRageStudios · Jul 13, 2014 at 01:37 PM
Thanks for the replies, that makes total sense.
So I figure than an easy way for me to do this might be to have the audio on the player, and just trigger the audio on the player before I destroy the object. Problem is I cant make it work in C#
Here is the player code
[System.Serializable]
public class soundEffects
{
public AudioClip key;
}
I have assigned the audio clip in the inspector
Then in the monoscript for the player
public soundEffects playerSounds = new soundEffects();
Then on the key object, use the following script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class ScriptObjectKeyPickup : MonoBehaviour {
ScriptPlayerOneManager script;
// Use this for initialization
void Start () {
script = GameObject.Find("GameObjectPlayerOne").GetComponent<ScriptPlayerOneManager>();
}
// Update is called once per frame
void Update () {
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "GameObjectPlayerOne")
{
script.playerInventory.playerOneKeyAmount += 1;
script.playerSounds.key.Play();
Destroy(gameObject);
}
}
}
It doesn't seem to like that Syntax of Play(); though. It gives me an error of CS1061: Type 'unity engine.AudioClip' does not contain a definition for 'audio' and no extension method 'audio' of type 'unityengine.AudioClip' could be found
Your answer
Follow this Question
Related Questions
How to destroy a gameobject by GameObject.Find 1 Answer
Destroying one shot sounds with rever components 0 Answers
Destroy on collision 2 Answers
Audio Artefacts When Stopping/Destroying during play. 2 Answers
Destroy object then remake object 1 Answer