1
votes

components attached I mean by name of each component.

var list = gameObj.GetComponents(typeof(Component));
                if (!componentsList.Contains(gameObj.name))
                    componentsList.Add(gameObj.name);
                for (int i = 0; i < list.Length; i++)
                {
                    if (!componentsList.Contains(list[i].name))
                        componentsList.Add(list[i].name);
                }

This code create a List only with GameObjects. But I want a List in this format for example:

Cube1 Mesh Renderer Box Collider Sphere Mesh Renderer Box Collider

In the end a List with each object name and the components of it.

2

2 Answers

4
votes

I assume when you say you need the name of the component, you actually mean the type. Component.name actually returns the name of the GameObject the Component is attached to, so it will be the same for all components.

You could do the following

    foreach(var component in GetComponents()) {
        Debug.Log(component.GetType());
    }

I wonder what you will need that for though ...

-1
votes
Component[] comps=gameObj.GetComponents();

List<Component> complist= new List<Component>();

for(inti =0;i<comps.length;i++){
complist.Add(comps[i]);
}