I have an audio source and an audio listener. In my script the audio clip is called by audio.Play() but there is no sound! The clip is okay, play on awake works.
Thanks
Coral
I have an audio source and an audio listener. In my script the audio clip is called by audio.Play() but there is no sound! The clip is okay, play on awake works.
Thanks
Coral
If you want other people to help you, you will need to give more info.
Maybe you could post your script!
Regards, Robert
var explosion : Transform;
var destroyed: int;
function DestroyYourself()
{
if(destroyed < 1)
{
Destroy (gameObject);
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
audio.Play();
Destroy(gameObject);
MoveAround.turrets -= 1;
destroyed = 1;
}
}
The sound is called by this script.
If the audio source is on the game object then when you destroy the game object the audio source will also be destroyed, I believe?
Oh, that’s right. But if I don’t destroy the object first (the object is a turret) it would keep shooting. Should I just put another object next to the turret and let it play the sound or is there a more elegant solution?
Well it seems that it’s quite common to place the audio source onto the main camera, however that might not be the method you want to go for.
So as far as I can tell having a specific audio source game object for the turret should take care of that problem but whether or not it is the most elegant method I do not know. ![]()
Also you might want to move:
MoveAround.turrets -= 1;
destroyed = 1;
To above Destroy(gameObject);
It shouldn’t be an issue. The object isn’t destroyed until the next frame, after all. Script should complete things after the Destroy().
You could also just put the turret destruction sound on your explosion object, since you’re creating an explosion there anyway.
If you are reusing the same explosion prefab elsewhere and don’t want it to sound like a turret exploding when you use it in other ways, then you could keep the AudioClip property attached to the turret but then assign that clip to the explosion object’s AudioSource.clip after you create the explosion (and you could slightly randomize the pitch at the same time to avoid having every turret explosion sound exactly the same – which is an advantage over just using AudioSource.PlayOneShot).
I had the same problem. AudioSource.PlayClipAtPoint seems to be the best solution. From what I can tell, it creates a temporary audio source at the transform position so that the clip can play through, then automatically destroys itself. ![]()