I’m trying to make a bullet play a sound when it hits a wall. The bullet currently has 2 AudioSources - one which plays immediately after a bullet is spawned (this one works) and another one called “Impact” which is supposed to play when I hit a wall. The problem is that I don’t know how to properly write the script to make it play that sound.
This is the bits of code that I have:
public class BulletController : MonoBehaviour
{
//some more variables
private AudioSource ImpactAudio;
// Use this for initialization
[UsedImplicitly]
private void Start()
{
//some extra things for the player
//I debugged this and it does assign the correct AudioSource
//to ImpactAudio
ImpactAudio = GetComponents<AudioSource>()[1];
}
[UsedImplicitly]
private void OnTriggerEnter2D(Collider2D otherCollider)
{
switch (otherCollider.name)
{
//some other cases
case "Block":
PlaySound();
//This part works - the bullets are actually destroyed by this
Destroy(gameObject);
BitBoy.ReloadBullet();
break;
default:
Debug.Log(otherCollider.name);
break;
}
}
//This should(?) play the Impact sound
private void PlaySound()
{
//This does write Impact in the console
Debug.Log("Impact");
//Nothing happens.
ImpactAudio.Play();
}
}
I am pretty new to Unity, just started working, but after searching through some previous questions related to this I still don’t have a solution.