AudioClips don't play from assets folder

If a clip is assigned directly to an AudioSource, it will play. If a clip is assigned via a string, it fails to play without manually prefixing the assets folder like so:

this.audioSource.play('assets/' + this.myAudioClip);

I discovered this when upgrading an old project from Needle 2.xx to the latest.

I’m assigning the clips in my component like this:

//@type UnityEngine.AudioClip
@serializeable()
hitFairwayClip: string;

Original Post on Discord

by user 300386587278049291

I think one way would be to serializing it as @serializable(URL) (the rest can stay as is).

Have you tried yet? :slightly_smiling_face: let me know

hey thanks for quick response - I won’t be able to try this until later today

by user 300386587278049291

hey that worked, thanks!

by user 300386587278049291

Great. As an explanation what is happening and why:

all referenced external assets will be written to the glb with a “glb relative path” so the audioclip is relative to the glb inside (usually) the assets folder “myclip.mp3”.

What the url parameter in the serializable does is it marks the string to be resolved relative to your web project (when you load the glb inside “my/long/path/scene.glb” it will also work and the mp3 will be “my/long/path/mysong.mp3”)

Similarly the glb and audio files could be hosted on another server with the glb and loaded into the engine and it would still work (https://domain.com/scene.glb and https://domain.com/mysound.mp3) - in cases where you feed external data into your project :slightly_smiling_face: or load external data in and out

I hope that makes sense.

Yea it does! Appreciate the explanation

by user 300386587278049291