Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 TheYokai · Oct 17, 2012 at 11:43 PM · c#audiosourcecomponent

Problems with Audio Sources

I recently decided to try to create a class which is (essentially) a song manager which would ideally allow me to create and manipulate songs to be different between each "loop" or change the song through the use of various triggers. Unfortunately, I ran into a bit of a snag -- the audio source components I attached to base object do not want to play.

I'll go step by step through the code:

     public AudioSource[] trackArray = new AudioSource[4];
     
     public float[] trackVolumeArray = new float[4];
     
     public float allTrackVolumeScale = 100.0f;
     
     public bool bClampValues = true;
     
     private int totalIndexOfVolumeArray;
     private int totalIndexOfTracksArray;

Nothing special here, I then initialize the variables in the Start():

 void Start () 
     {
         int trackArrayIndex = 0;
         int volumeArrayIndex = 0;
         
         // Initialize the Audio Source files..
         foreach( AudioSource track in trackArray )
         {
             trackArray[trackArrayIndex] = gameObject.AddComponent ("AudioSource") as AudioSource;
             track.volume = 1f;
             track.enabled = true;
             trackArrayIndex++;
         }
         
         // Initialize the Volume Settings..
         foreach( float trackVolume in trackVolumeArray )
         {
             trackVolumeArray[volumeArrayIndex] = 100f;
             volumeArrayIndex++;
         }
         
         totalIndexOfTracksArray = trackArrayIndex;
         totalIndexOfVolumeArray = volumeArrayIndex;
     }

Side Note: Earlier I tried to set each float by referring to it as "trackVolume" to no success, since you cannot redefine it. Is it wrong to use a foreach and not actually use each component your referencing?

Lastly, in the update function, I call a "check playing" function, which for testing purposes I had to change to try to be more direct with no results. The ideal way of doing this would probably be another "foreach".

     private void CheckPlaying()
     {
         if( trackArray[0].isPlaying != true )
         {
             trackArray[0].Play();
         }
         
         if( trackArray[1].isPlaying != true )
         {
             trackArray[1].Play();
         }
     }

I should also tell you that the AudioSource array has inputs in the inspector, and all I did from there is reference a audiosource that exists in the scene and is a child of the SongManager game object. Obviously, those AudioSources have an AudioClip attatched to them.

Is there any reason why this wouldn't be working?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by svenskefan · Oct 18, 2012 at 06:50 AM

Hi! what types of error messages do you get in the console? What do you see happening in the editor?

I am betting that you don´t see several audisources added to the gameobject; Unity wont let you add the same component twice to the same gameobject. the line: "trackArray[trackArrayIndex] = gameObject.AddComponent ("AudioSource") as AudioSource;" wont work.

(workaround is the create child gameobjects and add the component to them instead, one per child)

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

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Multiple Cars not working 1 Answer

How to destory gameObject without destroying the audio source attached to it? 2 Answers

Distribute terrain in zones 3 Answers

Help removing an AudioSource component from another script 0 Answers

How to disable script in other object 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