- Home /
 
Play An Audio Clip When An Instantiated Object Collides
I want to make bullets make a sound when they come in contact with a wall. I instantiate them as a prefab. I have attached a script to the bullet prefab that makes it explode and destroy itself but it doesn't make a sound. I have an audio source attached to my bullet. I have tried many things but I cant seem to get the sound to play. Here is the code :
`var explosion : Transform;
public var sound : AudioClip;
function OnCollisionEnter(collision : Collision) {
 if(collision.gameObject.tag=="Wall"){ audio.PlayOneShot(sound); Destroy (gameObject); Instantiate (explosion, transform.position, transform.rotation);
 
                }
 
               else {
 
                audio.PlayOneShot(sound);
 Destroy (gameObject);
 Instantiate (explosion, transform.position, transform.rotation);    
  
               } 
'
Answer by MC HALO · Apr 14, 2011 at 07:58 PM
Hey give this a try :
 var explosion : Transform;
 
                public var sound : AudioClip;
 
                function OnCollisionEnter(collision : Collision) {
 
               if(collision.gameObject.tag=="Wall"){
 AudioSource.PlayClipAtPoint(sound, transform.position); Destroy (gameObject); Instantiate (explosion, transform.position, transform.rotation);
 
                }
 
                else {
 
                AudioSource.PlayClipAtPoint(sound, transform.position); Destroy (gameObject); Instantiate (explosion, transform.position, transform.rotation); 
 
                } 
 
               
               try and see if this works :) if it does not work please let me know thanks :)
Your most welcome glad that i could help :) if you have any other problems contact me on my email gto_oni-eyes@hotmail.com
Your answer