Hi Folks,
I have the following newbie problem and its getting frustrating so Im asking you in the hope someone has a hint:
I have two scripts: AudioClipPlayer is attached to an empty gameobject and has an array loaded up with AudioClips. The second scipt has been added multiple Sprite objects and it supposed to call the AudioPlay() function in the AudioClipPlayer whenever the sprite object receives a click.
Calling the AudioPlay() script internally within the class working fine - see the commented section in AudioClipPlayer:
using UnityEngine;
using System.Collections;
public class AudioClipPlayer : MonoBehaviour {
public AudioClip[] audioarray;
public void Start()
{
audioarray = new AudioClip[]
{
Resources.Load("bark") as AudioClip,
Resources.Load("meow") as AudioClip
};
//the following line of code successfully play an audioclip:
//PlaySound();
}
public void PlaySound()
{
audio.PlayOneShot(audioarray[1]);
}
}
Here is the script ( stripped down version ) which is attached to the Sprite objects:
using UnityEngine;
using System.Collections;
public class AnimalScript : MonoBehaviour
{
AudioClipPlayer audioClipPlayer = new AudioClipPlayer();
public Sprite newSprite;
public void OnMouseDown()
{
audioClipPlayer.PlaySound();
}
}
however calling the PlaySound() from this class fails with the following error message:
NullReferenceException
AudioClipPlayer.PlaySound () (at Assets/AudioClipPlayer.cs:49)
AnimalScript.OnMouseDown () (at Assets/AnimalScript.cs:39)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)
Hope someone can give me a hint -
Thank you guys!