- Home /
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!
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.
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;
}
}
}
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.
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.
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...
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
![](https://koobas.hobune.stream/wayback/20220612152620im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What's the most efficient way to change script from child object 2 Answers
How to get Component from Game Object in C# 1 Answer
transform.root and empty root game objects don't play nice 1 Answer
how do I change the properties of a material that are on the children of an object on script? 1 Answer
GetComponent in parent question 3 Answers