How would I figure out what type a component is,Getting the type of a component
This is probably a dumb question but how would I go about checking what a component is from a generic component in C# Unity. I am trying to get a list of all components linked to objects like this
public List<Component> components = new List<Component>();
How would I find the type of that specific component, such as if it was a rigidbody, audiosource, collider, etc...
In my code, I am trying to get information from most of the game objects in a scene (such as the components attached to them) but have not found any way of doing it.
Answer by Vicarian · Jun 28, 2018 at 09:27 PM
foreach (Component c in components)
{
Debug.Log(c.GetType().ToString());
}
All types in C# are qualified by their namespace, so you'll get UnityEngine.Light, or UnityEngine.Camera as results. You can then use those fully qualified type names to reflect over the type and do some dynamic access.
thank you so much, I must just be kind of tired right now. Thanks for the quick response :)