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
1
Question by metaphysician · Jan 19, 2014 at 11:44 AM · c#audiosourcecastinggetcomponents

proper C# casting for GetComponents on Audio Source?

hey there - kinda tearing my hair out on the proper C# casting to return an array of Audio Sources. it may be that Unity 4 may have changed format a bit.

i set up my audio source variables in the usual way:

 AudioSource source1;
 AudioSource source2;
 private AudioSource[] sources;
 public GameObject playlistObj;    

 void Start(){
 sources = (AudioSource[])playlistObj.GetComponents(typeof (AudioSource));
 source1 = sources[0];
 source2 = sources[1];
 }

but this method while giving no syntax errors, returns a failed cast and then a null result later on.

i've seen some examples of adding AudioSources to a GameObject, but i'm more interested in retrieving a list of Audio Sources from a given object.

help appreciated!

scott

Comment
Add comment · Show 3
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 Benproductions1 · Jan 19, 2014 at 11:56 AM 0
Share

You should use the generic overload of GetComponents

avatar image metaphysician · Jan 19, 2014 at 12:36 PM 0
Share

is there a place that that overload is posted, or is that something i'd find in $$anonymous$$SDN or DotNetPerls?

avatar image Benproductions1 · Jan 19, 2014 at 12:42 PM 0
Share

The Unity docs, like everything else in the Unity API

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Blaveloper · Jan 19, 2014 at 12:08 PM

Actually, that's anything but usual. You're simply overdoing stuff, which is not needed at all.

All you need to do is the following:

 public AudioSource[] sources = new AudioSource[1];

You can then drag and drop all audio sources through the inspector in a list. You don't need to worry about the "1" part, thanks to the fact it's public you can adjust it via the inspector at any time.

Comment
Add comment · Show 7 · 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 Benproductions1 · Jan 19, 2014 at 12:09 PM 0
Share

This doesn't answer the question and it is definitely usual to use GetComponent! There is no reason to do something manually when you can do it via code

avatar image Blaveloper · Jan 19, 2014 at 12:11 PM 0
Share

No need to be rude, I was just giving proper advice.

avatar image Benproductions1 · Jan 19, 2014 at 12:14 PM 0
Share

I wasn't trying to be rude (and neither do I think I was), I was simply pointing out why your answer is not appropriate for this question.

avatar image metaphysician · Jan 19, 2014 at 12:23 PM 0
Share

there's one issue with the answer that you're posting. this is for an object that contains more than one AudioSource component on it. so i need to know the order of AudioSources and assign them accordingly. hence the casting for a list of Components. if every AudioSource i was looking for was on a separate GameObject, i could use your method.

so, i'm just really curious in C# if you have multiple copies of one type of Component on a specific GameObject, how do do you return the list of those Components so you can order them and then address them specifically?

avatar image Blaveloper metaphysician · Jan 19, 2014 at 12:28 PM 0
Share

Do you mean it as a comment to me? If so, I simply make multiple objects with one audio source each. Then I drag each object to the array list I specified before through the inspector.

avatar image metaphysician metaphysician · Jan 19, 2014 at 12:34 PM 0
Share

i understand your method and i personally agree with the basic idea (one game object for each Source). however i'm using another developer's tool. they have an object that has two AudioSources on it, in order to crossfade audio between two music clips. so i need to be able to address either Source independently.

avatar image Benproductions1 metaphysician · Jan 19, 2014 at 12:43 PM 0
Share

This is not an answer to the question. Please post comments as comments and not as answers.

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

AudioClip stuttering while loop is OFF and not using function Update? 1 Answer

Change AudioSource output via Script 1 Answer

Audio Manager or not? Audio System 0 Answers


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