2
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ might be worth adding in a clip manually like discussed just to try establish where the problem is coming from \$\endgroup\$ Nov 28, 2017 at 15:40
  • \$\begingroup\$ It would also help if you post a screen grab of the AudioSource settings. \$\endgroup\$
    – Stephan
    Nov 28, 2017 at 15:42
  • \$\begingroup\$ could you maybe use: public void PlayMe(AudioClip clip){ }. And then ensure clip is loaded before calling PlayMe(clip); \$\endgroup\$ Nov 28, 2017 at 15:43
  • 1
    \$\begingroup\$ Also make sure you have an AudioListener on your camera. The Editor may be using a default listener when you manually play the sound, but in-game if there isn't a listener, you wont hear anything. \$\endgroup\$
    – Stephan
    Nov 28, 2017 at 15:44
  • \$\begingroup\$ So this is a little strange, as I added the listener in the Start method: 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\$
    – Dtb49
    Nov 28, 2017 at 15:46

2 Answers 2

0
\$\begingroup\$

Because I am changing the Audio Clip during runtime the normal OnClick method doesn't catch my updated variable and only gets the original empty AudioSource.

In order to solve this, I added a listener to the button OnClick method in a script attached to my game object.

button.onClick.AddListener(delegate { MethodToRunOnButtonClick(); });
\$\endgroup\$
1
  • \$\begingroup\$ Do you need the extra delegate wrapper, or would AddListener(MethodToRunOnButtonClick) work too? Remember to mark this answer as Accepted if it solved your problem - click the checkmark icon to the left. \$\endgroup\$
    – DMGregory
    Nov 29, 2022 at 17:23
-1
\$\begingroup\$

Make sure you have an AudioListener on your camera. The Editor may be using a default listener when you manually play the sound, but in-game if there isn't a listener, you wont hear anything.

\$\endgroup\$
2
  • \$\begingroup\$ You didn't even answer my question nor was it even part of the question I asked. You obviously don't understand the difference between an AudioListener Component and an OnClick Listener for a button. \$\endgroup\$
    – Dtb49
    Nov 28, 2017 at 16:49
  • \$\begingroup\$ The OnClick is working or you wouldn't be getting your debug statements. If you were failing to get a handle to your AudioSource, you'd be throwing NPE's, which you aren't. And if you failed to load you AudioClip, you wouldn't get your second Debug. You said your issue was that you couldn't hear the sound when it played. This is almost always the case when a developer forgets to add an AudioListener component to an object in the scene. Based on your comments above, you forgot to add a listener. Now your problem is the discrepancy between device and editor, which is another question. \$\endgroup\$
    – Stephan
    Nov 28, 2017 at 16:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .