Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 changled · Feb 14, 2018 at 12:32 PM · getcomponentaudiosourcegetcomponentinchildren

How to call GetComponent by name? GetComponents?

Is the only option for using GetComponent() to get more than one component of the same time GetComponents()? For example, if I have several audio files I want to play depending on the scenario, do I have to:

 public class ExampleClass : MonoBehaviour {
     public AudioSource[] audios;
 
     void Example() {
         audios = GetComponents<AudioSource>();
             
         if (_condition_)
             audios[0].Play();
         else
             audios[3].Play();
     }
 }

If that's the case, I seem to only be receiving the audio file for the first AudioSource component (audios[0]) and none of the others. Does it not pass in order of component in the inspector for the GameObject? Is there no way to call by audio name or something? If not, can someone explain why, as this seems so senseless to me! Thank you very much!

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 Bonfire-Boy · Feb 14, 2018 at 02:56 PM 2
Share

I$$anonymous$$O it would make more sense to handle audio file/clip selection by having a single AudioSource on your GameObject, and changing its AudioClip.

If you really want to continue with multiple AudioSources, you could establish the references yourself. For example if you want to play a clip with a certain name, you could search your audios array for the AudioSource that has a clip of that name.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by itchyOwl · Feb 14, 2018 at 03:29 PM

Well, GetComponents will always return an array, but that should not restrict you. You can use a dictionary instead. Instead of using multiple sources, you might want to use one source and multiple clips like so:

 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 
 public class ExampleClass : MonoBehaviour
 {
     public AudioSource source;
     public List<AudioClip> clips = new List<AudioClip>();
     private Dictionary<string, AudioClip> clipsByName;
 
     void Example()
     {
         source = GetComponent<AudioSource>();
         // Either seek directly from the list
         source.clip = clips.FirstOrDefault(c => c.name == "ClipName");
         // Or use a dictionary
         clipsByName = clips.ToDictionary(c => c.name, c => c);
         AudioClip clip;
         if (clipsByName.TryGetValue("ClipName", out clip))
         {
             source.clip = clip;
         }
     }
 }
   
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
avatar image
1

Answer by ransomink · Feb 15, 2018 at 01:17 AM

GetComponents() will get all audio source components on the game object attached to this script. If there is only one audio source then it will only have one in the array. They will enter the array in order in the inspector.


By the way, check if the audio source is null before trying to access its fields.

Comment
Add comment · Show 3 · 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 itchyOwl · Feb 15, 2018 at 08:34 AM 0
Share

It depends. If the source should not be null, you'll want the exception or a custom warning/error msg from it. If it's allowed that the source is null, then you should make the null check before accessing it. Overmanaging errors can lead to unspotted bugs.

avatar image ransomink itchyOwl · Feb 15, 2018 at 03:39 PM 0
Share

Regardless, they need to check if it's null. Either throw an error when it is or if it can be null, go to the next index in the array. This helps figure out if they have an audio source or why the clip isn't playing...

avatar image Bonfire-Boy ransomink · Feb 15, 2018 at 04:35 PM 0
Share

No, if you want it to throw an exception then you don't need to check that it's null before accessing it.

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

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

Get Components without parent? 2 Answers

How to get script from Object with (Clone)'s script? 2 Answers

How to get first depth child components of a parent object? 7 Answers

GetComponentInChildren, is there an Alternative way? 1 Answer

Microphone.GetComponent () not working + Microphone.devices empty? 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