- Home /
Audio sound on gameObject delete?
I have a ray caster that deletes a specific type of object, once deleted I want to play an audio track. I have it all set up but when the object is deleted it can't find an AudioSource to play, now I've tried linking the audio to another gameObject but it is unable to find it. If I add the audio to the objects I wish to delete then once the first goes it can no longer find the audio.
var PageCount : GUIText;
var Count : int;
var pickup : AudioClip;
function Touched(hit:RaycastHit)
{
Destroy(gameObject);
}
function Update () {
if ( Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 0.8))
{
hit.transform.SendMessage("Touched", hit, SendMessageOptions.DontRequireReceiver);
Count ++;
PageCount.guiText.text = Count.ToString();
audio.Play();
audio.loop = false;
}
}
}
Answer by DaveA · Oct 13, 2012 at 05:30 PM
Make a game object that is separate and has the audio src and clip and never is deleted, and all other objects which can delete use that object to play their sound. If you put sound on objects which can be deleted, they may delete before the sound plays.
Noob question but when I add an audiosource to a game object, one that isn't deleted I can't find it in the audio selection on the script? It only shows files from the Project window...