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);
}
}
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.
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
);
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.)
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.