I currently have a working script that streams audio using a single audiosource.
I now want to layer some one shots over this streamed audio and I am struggling.
My current WORKING code looks somewhat like this…
var audioSource: AudioSource;
var myAudioClip: AudioClip;
var started: boolean;
//starts streaming intro music
function Start ()
{
www = new WWW ("file://" + Application.dataPath.Substring (0, Application.dataPath.LastIndexOf ("/")) + "/Assets/intro.wav");
myAudioClip = www.audioClip;
audioSource = GetComponent(AudioSource);
audioSource.audio.clip = myAudioClip;
audioSource.loop = false;
started = false;
}
function Update()
{
if(!audioSource.audio.isPlaying && audioSource.audio.clip.isReadyToPlay && !started)
{
audioSource.audio.Play();
started = true;//quick reference to know audio has started
}
}
Now whilst this music plays I want to fire oneshot audio. From what I gather I should use seperate audio sources. So I created two empty gameobjects, named them AudioLeft and AudioRight and attached audiosource components to both.
I add to the start of the script…
var audioSource2: AudioSource;
var myAudioClip2: AudioClip;
and assign the myAudioClip2 with a sample loaded into memory from the unity UI.
I then tried to initialise the audio clip in the start method using
audioSource2 = GameObject.Find("AudioLeft").GetComponent(AudioSource);
audioSource2.audio.clip = myAudioClip2;
Debug.Log(audioSource2.audio.clip.length);
as well as playing the clip later upon a keypress using…
showDebugText("<b>Action:</b> Playing One Shot");
audioSource2.audio.PlayOneShot(myAudioClip2);
Whilst the Playing One Shot debug text appears. It seems to be giving me an Object Reference Not Set to the Instance of an Object for the line Debug.Log(audioSource2.audio.clip.length);
So for starters my assignment of audioSource2 isn’t working. I’m pretty new to all this and feel like I’ve probably made some fundamental errors so go easy on me please
Thanks in advance!