• 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 /
avatar image
0
Question by Shivarana · Jun 14, 2017 at 08:29 AM · audio source

Changing AudioSource options with script.

Hello there everyone,

I usually really try to find answers by myself, digging here or on forums and Youtube but I can't seem to find anything about this.

I'm working on an AudioManager with a sounds library. At the moment I only have 2 music themes and I ask my code to create new GameObjects (my musicSources) and add an AudiSource to each of them. The whole thing works perfectly, swapping and crossfading between scene, volumes, ... But as they are code-generated AudiSources, the loop is not ticked and I can't change it as the objects don't exist before I press Play. The music doesn't loop.

Is there a line of code to modify this? Or maybe should I go around and Instantiate Prefabs of these musicSources?

Here is my code for this section

 {
             instance = this;
         DontDestroyOnLoad (gameObject);

         library = GetComponent<SoundsLibrary> ();

         musicSources = new AudioSource[2];
         for (int i = 0; i < 2; i++) 
         {
             GameObject newMusicSource = new GameObject ("Music source " + (i + 1));
             musicSources [i] = newMusicSource.AddComponent<AudioSource> ();
             newMusicSource.transform.parent = transform;
         }
 ``}
                     




Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

65 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 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 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 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 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

Lower volume when another audio source plays 1 Answer

Custom inspector curves similar to the 'Audio Source' volume/pan/spread Curves 0 Answers

how can i mute all audio sources in the scene? 2 Answers

Is there a more efficient way to trigger an audio source from an array to play than using a timer? 0 Answers

Array of AudioClips not playing, please help 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges