In my shoot em up game I have successfully scripted two weapons that the player can switch between. The problem is I want each weapon to sound different, and I cant figure out how to set this up. In Unity if I attach two audiosources to the player they are both called ‘Audiosource’, how can I tell unity which one to play?
You give them different names.
Components don’t have names.
Why do you need different AudioSources? Can’t you just change the clip the source is playing?
Given the limited information, it sounded like he didn’t really name his variables in his scripts right.
I’ll be honest, ive only been using Unity for a few weeks, and I thought that an audiosource had to have just one clip assigned, how can I make unity switch between two clips? This is my Player Controller script:
public class PlayerController : MonoBehaviour
{
private bool WeaponA;
private bool WeaponB;
void Start()
{
WeaponA = true;
WeaponB = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
if (WeaponA == true || WeaponB == false)
{
WeaponA = false;
WeaponB = true;
gameObject.GetComponent<WeaponSelect>().enabled = false;
gameObject.GetComponent<WeaponSelect2>().enabled = true;
}
else
{
WeaponA = true;
WeaponB = false;
gameObject.GetComponent<WeaponSelect>().enabled = true;
gameObject.GetComponent<WeaponSelect2>().enabled = false;
}
}
}
To set which clip will play by default
To play a specific clip without changing the default clip
You just need to store which clip to play for which gun somewhere. That’s up to you
Thanks a lot dude, its all working now ![]()