• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
3
Question by thedodgeruk · Oct 31, 2012 at 01:52 PM · audiosource

create audioSource via script?

i tryed this but not work , why

  private AudioSource m_audioSource;            
 
 start()
 {
 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 ?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thedodgeruk · Oct 31, 2012 at 01:55 PM 0
Share

found the problem , it should have been

m_audioSource = gameObject.AddComponent();

cause its a Component

1 Reply

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by Tim-Michels · Oct 31, 2012 at 01:55 PM

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

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 ;)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thedodgeruk · Nov 01, 2012 at 12:29 PM 0
Share

to use Resources.Load(name) do you need the prefab in be in a specific folder?

avatar image Tim-Michels · Nov 01, 2012 at 12:45 PM 1
Share

Resources.Load loads assets from an existing Resourses folder under your Assets folder. (Assets/Resources)

You can still have subfolders like Assets/Resources/Textures

Then you will need to do something like this: Resources.Load("Textures/diffuse");

avatar image tkamruzzaman · Oct 22, 2015 at 11:09 AM 0
Share

perfect answer :) thnx @Tim $$anonymous$$ichels

avatar image mahuja · Oct 14, 2017 at 10:54 PM 0
Share

For those on 2017.1 version this is a solution which works for first line, as per updated API for library function for AddComponent

 var audioSource : AudioSource = gameObject.AddComponent.<AudioSource>() as AudioSource;

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Playing looped sound while holding button is distorted 2 Answers

Audio after changing product name 1 Answer

No GetOutputData read when there is more then 31 AudioSources 0 Answers

AUDIO DATA to STATIC WAVEFORM DISPLAY? 1 Answer

Playing multiple sounds at the same time? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges