1
votes

I am updating an AudioSource in Unity3D during runtime when I click.

I created a Cube game object.

When I click the cube I send properties to my AudioSource.

NOTE: this is to eventually play many other wav files that are passed in via a string array of pipe delimited wav files (which exist in the Assets Audio folder).

So I need to update the AudioSource with each CLICK EVENT whereby each time it will load a new file selected from the array.

Okay, the Unity3D Editor shows my public variables from the script, and it fills them out at runtime (upon clicking the cube.)

My path variable shows up perfectly in the Unity3D editor.

But the problem is although the Audio Clip property in the editor shows the small orange audio logo (as if something is there/loaded), the name of the file that I grabbed from my Assets/Audio folder is always empty.

Nothing plays, but I get no errors!

And the properties playOnAwake, volume, etc. all show me exactly what I express them to do via my script.

NOTE: In the first image, the path variable correctly points to the .wav file.

Why is my .wav not playing?

Why is the .wav filename not appearing in the Audio Clip text box area?

My publicly declared variables in the script.

My created Audio Source.

public Color clickColor;
public Color releaseColor;
public int clickcount = 0;    
public WWW www;
public AudioClip myAudioClip;
public string path;

void CubeClicked(GameObject tmpGameObject)
{
    // CLICK COUNT
    clickcount++;

    // CLICK COLOR
    tmpGameObject.GetComponent<Renderer>().materials[0].color = clickColor; 

    // IS TRIGGER
    tmpGameObject.GetComponent<Collider>().isTrigger = true; 

    //AUDIO
    path = "file://" + Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("/")) + "/Assets/Audio/JetEngine.wav";
    www = new WWW(path);
    myAudioClip = www.audioClip;

    tmpGameObject.GetComponent<AudioSource>().clip = myAudioClip;
    tmpGameObject.GetComponent<AudioSource>().playOnAwake = false;
    tmpGameObject.GetComponent<AudioSource>().volume = 0.999f;
    tmpGameObject.GetComponent<AudioSource>().Play();

}
1
did you checked the audio listner ?Tiago Fabre
try doing this : yield return www; after the new WWW(). Also check that your volume is enabled.Cabrra
Cabbra your advice put me on the right track. It works now. :)anthony_s

1 Answers

2
votes

Cabbra's suggestion was what I needed. Adding "yield return www;" forced me to use the IEnumerator and magically, it started to work!

StartCoroutine(playMusic(tmpGameObject));  

public IEnumerator playMusic(GameObject tmpGameObject){
    /*Same code from before goes here!*/
}