- Home /
Audio API in Unity 5 creating issues (cannot play two audioclips anymore)
Forgive me if the answer is already out there, but I did do my research before asking, and I have not found an answer.
I have a simple function playing one sound when the mouse is over and another sound when the mouse is clicking.
In Unity 4, this would be quite simple. This is what it would look like in Unity 4.
var track1 : audioclip;
var track2 : audioclip;
function OnMouseOver(){
audio.clip = track1;
audio.Play();
if(Input.GetMouseButtonDown(0)){
audio.clip = track2;
audio.Play();
}
}
In Unity 5, however, it is not as nice. When I try to type that, it tells me to let it upgrade the API and it comes out this. (I must let it do that or it will not let me run the game)
var track1 : audioclip;
var track2 : audioclip;
function OnMouseOver(){
GetComponent.<AudioSource>().clip = track1;
GetComponent.<AudioSource>().Play();
if(Input.GetMouseButtonDown(0)){
GetComponent.<AudioSource>().clip = track2;
GetComponent.<AudioSource>().Play();
}
}
The problem with this is that it requires me to add an Audio Source component to the GameObject the script is on. And that means I can only have one Audio Clip on the Object.
I could supposedly do this with two separate GameObjects, but that would require adding more GameObjects and more variables and if I could find an easy solution to this, it would be very nice.
Alright so I lied. You can have two Audio Source Components. So then my question is, can I do this without having a stupid Audio Source component?