I have tried this:
Magic.AddComponent(AudioSource);
var OAudio = Magic.GetComponent(AudioSource);
OAudio.audio.clip = Resources.Load("Assets/Sounds/WaterMagic");
and It doesn’t add the clip to the AudioSource component how can I fix this?
I have tried this:
Magic.AddComponent(AudioSource);
var OAudio = Magic.GetComponent(AudioSource);
OAudio.audio.clip = Resources.Load("Assets/Sounds/WaterMagic");
and It doesn’t add the clip to the AudioSource component how can I fix this?
So move your audio file to the Assets/Resources/Sounds directory (I’m assuming it’s called “WaterMagic.mp3”), then load it with:
audio.clip = Resources.Load("Sounds/WaterMagic");
When you call “audio” on a component or game object it’s just an AudioSource, so OAudio.audio.clip
should be replaced with OAudio.clip
. Your problem is that you’re not calling Play (even if the AudioSource has “PlayOnAwake” by default, though Awake is called when you call AddComponent, so when you later assign the clip it’s already been called):
Magic.AddComponent(AudioSource);
var OAudio = Magic.audio;
OAudio.clip = Resources.Load("Assets/Sounds/WaterMagic");
OAudio.Play();
Besides that, make sure you’re using the right path, you’re looking for the folder “Assets/Sounds/WaterMagic” (“Assets” searched by Resource.Load
is not the folder “YourUnityProject/Assets”, but “Resources/Assets”).