Playing sound while turret shoots? (resolved)

I know how to add an audiosource to an object, but how do I play a firing sound while it shoots?

Unity - Scripting API: AudioSource.PlayOneShot :wink:

Thanks! It looks like it should work, but I can’t figure out where to put it.

Here’s my collision script, in which a worm shoots at the turret. It just makes the turret blow up after getting hit. I don’t know where to add the PlayOneShot part.

var explosion : Transform;


function OnTriggerEnter( hit : Collider )
{
	
	if(hit.gameObject.tag == "wormProjectile")
	{
		Destroy(hit.gameObject);
		var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
		Destroy(gameObject);
		
	}


}

here you go:

var explosion : Transform;
var hitSound : AudioClip;

function OnTriggerEnter( hit : Collider )
{
   
   if(hit.gameObject.tag == "wormProjectile")
   {
      audio.PlayOneShot(hitSound);    
      Destroy(hit.gameObject);
      var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
      Destroy(gameObject);
      
   }
}

hope it helps :slight_smile:

Now, my projectile is just going through my turret. It’s really weird, because it looks like the audio part shouldn’t affect collision. :?

Do you mean the turret doesn’t get destroyed when it should? Are you getting any error messages at the time when the projectile hits? An error might cause the function to abort before Destroy gets called.

The turret normally destroys how I want, but when I add the audio part to the script, my projectile just goes through him. No collision and he’s not being destroyed. And the sound doesn’t play.

Are there any error message in the console?

“NullReferenceException
TurretControl.Shoot (System.Object seconds) (at Assets\script\TurretControl.js:30)
TurretControl.Update () (at Assets\script\TurretControl.js:20)”

Here’s the script it’s talking about:

var LookAtTarget:Transform;
var damp = 6;
var bullitPrefab:Transform;
var savedTime=0;


function Update () 
{
	if(LookAtTarget)
	{
		var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
		
		transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
	
		var seconds : int = Time.time;
		var oddeven = (seconds % 2);
		
		if(oddeven)
		{
			Shoot(seconds);
		}
	}
}

function Shoot(seconds)
{
	if(seconds!=savedTime)
	{
		
		var bullit = Instantiate(bullitPrefab , transform.Find("spawnPoint").transform.position , 
										Quaternion.identity);
		bullit.gameObject.tag = "enemyProjectile";							
		bullit.rigidbody.AddForce(transform.forward * 1000);
	
		savedTime=seconds;
	}
}

Is ‘bullitPrefab’ assigned?

Yes, it’s assigned to a fireball prefab.

Maybe it’s not finding the ‘SpawnPoint’ game object.

Try breaking the code down into separate steps, so that you can check the bullet prefab, the bullet game object, and the ‘spawn point’ game object against null individually, and add debug output indicating whether any of them is invalid. That should help in isolating the cause of the problem.

There’s no reason it shouldn’t find spawnPoint. I’m trying to play a sound when the turret blows up. When I have the play sound part in the script, the projectile goes right through the turret. When I take out the play sound part, I don’t get any errors, and the turret succesfully blows up.

I’m confused - you said that playing an audio clip is causing the problem, but I don’t see anything related to playing an audio clip in the code you posted.

As far as I can tell, the exception message you posted refers to this line:

var bullit = Instantiate(
    bullitPrefab,
    transform.Find("spawnPoint").transform.position, 
    Quaternion.identity
);

Is that not correct?

It DOES say the error occurs there, when I type in the audio clip part of another script.

It finds the error in the “TurretControl” script. But I’m working with the “TurretCollision” script.

This is how I started my TurretCollision (without sound):

var explosion : Transform;

function OnTriggerEnter( hit : Collider )
{
	
	
	if(hit.gameObject.tag == "wormProjectile")
	{
		Destroy(hit.gameObject);
	
		var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
		Destroy(gameObject);
		
	}
}

Then I change it to this:

var explosion : Transform;
var hitSound : AudioClip;

function OnTriggerEnter( hit : Collider )
{
	
	
	if(hit.gameObject.tag == "wormProjectile")
	{
		audio.PlayOneShot(hitSound);
		
		Destroy(hit.gameObject);
	
		var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
		Destroy(gameObject);
		
	}
}

All I change is adding the audio clip part. When I do that, the following errors occur:

  1. It doesn’t play the audio clip I assigned to hitSound.
  2. The fireball is not sensing collision with the turret (goes right through).
  3. There’s an error in the “TurretControl” script.

Hm, I have to admit it’s not immediately obvious to me what the cause of the problem is (or might be). However, even if you’re sure that nothing could possibly be going wrong in the line in question, I would still recommend adding some debug output to ensure that at that point ‘bullitPrefab’ is valid and the ‘spawnPoint’ game object is actually found.

Beyond that though, I think I’d have to see the actual project in order to figure out what’s wrong. (Maybe someone else will be able to spot the problem though.)

Does the gameobject with the TurretCollision script has an AudioSource component ?

Actually, when I add an audio source, it detects collision and the explosion works, BUT the audio isn’t being played.

Here’s the settings of the audio source on my game object:

Audio Clip : gul1 (just for testing purposes)
Play on awake : no
Volume : 10
Pitch : 1
Min Volume : 10
Max Volume : 10
Rolloff Factor : 0.1
Loop : no

Here are the settings for my collision script on my game object:

Script : TurretCollision
Explosion : Small Explosion
Hit Sound : gul1

Here’s the collision script:

var explosion : Transform;
var hitSound : AudioClip;


function OnTriggerEnter( hit : Collider )
{
	
	
	if(hit.gameObject.tag == "wormProjectile")
	{
		audio.PlayOneShot(hitSound);
			
		Destroy(hit.gameObject);
	
		var exp = Instantiate(explosion, gameObject.transform.position, 
						Quaternion.identity);
		Destroy(gameObject);
		
	}
}

I’m getting no errors in the Console.

Try replacing:

audio.PlayOneShot(hitSound);

With:

AudioSource.PlayClipAtPoint(hitSound, transform.position, 10f);

And removing the AudioSource component from the game object. (You may have to change the syntax for the ‘10f’ - I’m not sure how floating-point literals are handled in UnityScript.)

As it is now, you’re starting playback of the sound, but then immediately destroying the game object to which it’s attached. This is most likely why you’re not hearing anything.

YES! That worked!!! Thank you so much for solving my long-lasting issue. :smile: