Basically, I am loading an Audio Clip onto a game object's Audio Source in the Awake function when the game starts. When I click on the game object a UI comes up that basically has a Play button attached and all I want to do is play the Audio Clip on the Audio Source. The problem is I can see that there is an Audio Clip on the Audio Source at runtime and I can play it in the inspector at runtime but, when I click the button nothing happens. I have tried just referencing the gameObject in the OnClick() of the UI button and that doesn't work and I have also tried going through and getting the gameObject through GameObject.Find
public void PlayMe()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("Voice_log");
foreach (GameObject obj in objs)
{
if (obj.layer == LayerMask.NameToLayer("Selected"))
{
Debug.Log("Found it");
if (obj.GetComponent<AudioSource>().clip.loadState == AudioDataLoadState.Loaded)
{
obj.GetComponent<AudioSource>().Play();
Debug.Log("Should Play");
}
}
}
}
Both of my Debugs print when I click the Play button.
button.onClick.AddListener(delegate { PlayMe(); });
and now it will work when I deploy it to the device but, for some reason it doesn't work in the editor unsure why though. \$\endgroup\$