[Solution]
Create a folder in your Assets called Resources and put the resources you need to load in it.
[/Solution]
I’ve not got much of my code left any more, I’ve rewritten multiple functions and tried multiple solutions but somewhere it is always going wrong. No matter what I do or what solution I turn to, audio only plays if the sound is attached to an object with an AudioSource. Loading resources into the AudioSource doesn’t work. I’m doing something wrong, or it just is not possible without the editor.
Broken script examples bellow(All I got left, pretty much):
public class AudioPlayer : MonoBehaviour {
public GameObject soundSource;
public AudioClip[] list;
void Start() {
list = new AudioClip[]
{
(AudioClip)Resources.Load("Sounds/hit1"),
(AudioClip)Resources.Load("Sounds/hit2.wav"),
(AudioClip)Resources.Load("Sounds/hit3")
};
}
public void PlaySoundAtObj(string sName,GameObject obj) {
int number = Random.Range(0,list.Length);
gameObject.GetComponent<AudioSource>().clip = list[number];
gameObject.GetComponent<AudioSource>().Play();
}
}
public static GameObject soundSource;
public static AudioClip[] sounds;
public static AudioPlayer Instance {get; private set; }
void Start() {
AudioClip[] sounds = (AudioClip[])Resources.LoadAll("Sounds",typeof(AudioClip));
}
public void Bounce(GameObject gobj){
PlaySoundAtObj("hit" + Random.Range(1,3).ToString(),gobj);
}
public void PlaySoundAtObj(string sName,GameObject gobj) {
foreach(AudioClip clip in sounds){
if(clip.name == sName) {
GameObject _sound = Instantiate(soundSource,gobj.GetComponent<Transform>().position,Quaternion.identity) as GameObject;
_sound.GetComponent<AudioSource>().clip = clip;
_sound.GetComponent<AudioSource>().PlayOneShot(clip);
Destroy(_sound,clip.length);
}
};
}
Code containers broke… not sure how or why…
(All problems encountered while in the Editors Game test function, application was never built)
- Resource.Load or LoadAll doesn’t work for AudioSource.clip = clip; The sounds loaded never play when using .Play(), AudioSource.PlayAtPosition(clip,transform.position).
- Instantiate Doesn’t really work at all for sound use. I worked with it for so long trying to get it to work but mostly loaded resources got unloaded and .clip value kept returning null. It may have worked for a while with the single clip loaded into the AudioSource, but it would never work for what I needed it to do. I’ve got 3 different sounds I want to play and additional sounds to play at other conditions. I tried various solutions found on the Unity forum and from StackOverflow, but neither worked.
- List or AudioClip[] : I used both of these and used various methods to try and load the sound resources into them, still the preloaded sound clips are never useable.
- I have gone through existing answers, or at least a good lot. Though to no avail.
Notes and thoughts:
The only thing that seem to work is AudioClips that are loaded into an object and then using .Play().
Perhaps .wav doesn’t work for Resource.Load or something, I’ve tried including and excluding the folder name and extension for the sound assets when using resource loading.
Also, perhaps I have to build for Resource.Load to even work to begin with.
I’m giving up on doing what I’ve been trying to do and will move on tomorrow, I’m currently doing a course for Unity game development and I wanted to try and make a more interesting scene than the current lesson intended.
It was clearly a huge mistake as I’ve spent over 10 hours on only the audio function now.
But please, if you know a way to load a list of sounds without publicly exposing mutliple variables for manual input, know how to get Resource.Load to actually pass the loaded sounds into objects and perhaps even how to Instantiate objects and load sounds into them. (PlayAtPosition-function isn’t very good since if there is no clip it never deletes the instance it creates.)
Time for me to grow my hair back and regain my sanity, cheers.