- Home /
Can play multiple sounds from one audiosource on an object. But cant play sound from audiosource on another?
I'm making a game for class with a tank that you can move and shoot. Relevant audio code on the Player Tank is this which is in a script called TankShooting attached to the tank object:
public class TankShooting : MonoBehaviour {
public AudioClip reload;
public AudioClip shoot;
if (Input.GetKeyDown("space")){
if (curammo > 0) {
gameObject.GetComponent<AudioSource> ().PlayOneShot (shoot, 1.0f);
}
So when you press space, you fire a bullet, and it plays the fire sound no problem. Also if you drive over an ammo box it'll play a reload sound. This works fine as well.
void OnTriggerEnter(Collider col){
if (col.tag == "AmmoBox") {
gameObject.GetComponent<AudioSource> ().PlayOneShot (reload, 1.0f);
}
}
The way the tank shoots is instantiating a prefab of a bullet object I have. The bullet has a sphere collider, rigidbody, its own audiosource, and a script to handle playing a sound when it hits something, and instantiating an explosion effect. The explosion works fine, but the sound won't play.
This is the script attached to the bullet prefab:
public class BulletScript : MonoBehaviour {
public AudioClip explode;
void OnCollisionEnter(Collision col){
gameObject.GetComponent<AudioSource> ().PlayOneShot (explode, 1.0f);
}
}
I've tried various Debug.Logs and everything seems to be working as it should. The scripts work and run but the sound just doesn't play. Am I missing something simple? The audio file is fine as I tried swapping it into the tank script and it plays.