Hello. I have recently written (with help from another question (: ) a script that deactivates gameobjects when a trigger is hit (for a 3.X version of unity). I have added a sound and audio source to the object and added a play audio in my script. The audio does not play.
Here is the script.
#pragma strict
var Item : GameObject;
var Sound : AudioClip;
function Start () {
Item.active=true;
}
function OnTriggerEnter(){
audio.PlayOneShot(Sound);
Item.SetActiveRecursively(false);
}
Have you assigned the sound file to both the Audio Source and Audio Clip in the inspector?
And are you sure the OnTriggerEnter() is working? You might want to set up some debug text to verify the event is working.
If you did all that right my only guess is that your SetActiveRecursively could be causing the problem. Or that your audio source is not set to be large enough to reach your Audio Listener.
You could try increasing the Audio Source “Min Distance” and “Max Distance” variables to ensure that your source will in fact reach your listener.
Also I have to ask why are you not using Unity 4.X?
They have changed the way the gameObjects are handled so SetActiveRecursively is no longer the way to go in 4.X maybe it’s not that great in 3.X either?
You may also want to try it without the :
Item.SetActiveRecursively(false);
That could cause the sound to not play as well possibly, I’ve never used SetActiveRecursively though so I’m just speculating.