create audioSource via script?

i tryed this but not work , why

 private AudioSource m_audioSource;            

m_audioSource = new AudioSource();
m_audioSource.loop = true;

and when it hit the “loop = true;” i get an error
i figure im not creating the AudioSource correctly .
the way im writing the code , it CANNOT be done via the inspector
so it has to be done via compleate script . any ideas how to do this ?

AudioSource audioSource = gameObject.AddComponent();
audioSource.clip = Resources.Load(name) as AudioClip;

You need to have a clip assigned. In this example, I use Resources.Load(), but you could just use an assigned AudioClip or something like that.

Cheers :wink: